...

View Full Version : onsubmit Function not called first time in IE



satsujin
08-04-2008, 11:27 AM
Hi, I'm pretty new to javascript so could someone please help me with this problem? I'm calling a custom function called validate() from a form's onsubmit handler through onsubmit="return validate()" . However,while this works in Firefox, the function is called the first time I submit instead it just submits immediately. However, following submits go to the function. Can someone please help? The function is just for testing as follows:

function validate(){
alert("yo");
return false;
}

abduraooft
08-04-2008, 11:29 AM
Can we have a link to see the real action?

satsujin
08-04-2008, 11:33 AM
I have the file on localhost. Is there somewhere I can upload it to?

abduraooft
08-04-2008, 11:36 AM
Then post your complete code(html+javascript) here.
PS: please follow http://www.codingforums.com/showthread.php?t=82672 on how to post your code.

satsujin
08-04-2008, 11:48 AM
Its a little bigger than I'd like so im cutting a few non-important bits out. I'm putting it all in code tags.


<HTML>
<head>
<script type="text/javascript">
var list1;
var list2;
var list1Clone;
var cloneOptions;

function createListObjects(){
list1 = document.getElementById("list1");
list2 = document.getElementById("list2");
//alert(list2.options.item(0).value);
//alert(document.forms[0].list1.options.item[0].value);
//list1Clone=list1.cloneNode(true);
//cloneOptions=list1Clone.getElementsByTagName("option");
//but = document.getElementById("test");
//but.onclick = test;

}
function delAttribute(){
var selIndex = list2.selectedIndex;
if(selIndex < 0)
return;
list2.removeChild(list2.options.item(selIndex));
}
function addAttribute(){
var addIndex = list1.selectedIndex;
alert(addIndex);
if(addIndex < 0)
return;
list2.appendChild(list1.options.item(addIndex));
}
function validate(){
alert("yo");
return false;
}
</script>
</head>
<body onload="createListObjects()">
<?php
if (isset($_POST['send'])){
print_r($_POST);
}
?>
<form method="post" name="add" action="test.php" onsubmit="return validate()">
<table border="1">
<tr>
<td><select size="4"
id="list1"
name="list1">
<option value="option1">
option1
</option>

<option value="option3">
option3
</option></select></td>

<td >
<button onclick="addAttribute()">
></button>
<br>
<button onclick="delAttribute()">
<</button>
</td>

<td><select size="4"
id="list2"
name="list2">
<option value="option2">
option2
</option>

<option value="option4">
option4
</option>
</select></td>
</tr>
</table>
<input type="submit" name="send" value="Send">
<input type="hidden" name="allopt">
</form>
</body>
</html>

satsujin
08-04-2008, 11:50 AM
This is all in a .php file by the way.

abduraooft
08-04-2008, 11:58 AM
It works for me as intended. No such problems.

satsujin
08-04-2008, 12:13 PM
Strange, I just tried rebuilding the same file piece by piece and it stayed working. I dunno what was wrong the first time. Thanks anyway. You must be good luck or something. :)

abduraooft
08-04-2008, 12:21 PM
Probably you forgot to refresh the page after adding the script for the first time. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum