Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

why doesnt this work 2

Status
Not open for further replies.

richardko

Programmer
Joined
Jun 20, 2006
Messages
127
Location
US
<html>
<head>
<SCRIPT language="javascript" type="text/javascript">
function check()
{
var ret="";
for(var x=1;x<=2;x++)
{
var element="chektu"+x;
//alert("elas "+element);
if(document.getElementById(element).checked)
{
alert("insiede");
ret="no";
}
}
if(ret=="")
{
return true;
}else{
return false;
}
}
</script>
</head>
<body>
<form name="Test" action="<? $PHP_SELF ?>">
<input type="checkbox" id="chektu1">
<input type="submit" name="original" value="Send Original Items As indicated Above" onClick="return check();">
</form>
</body>
</html>



I dont want the form to be submitted if the button is checked but it does get submitted. Any idea why?
thanks
 
remove the call to your function in the submit button. instead, call the function from the form's onsubmit event:

Code:
<form ... onsubmit="return check();">



*cLFlaVA
----------------------------
[tt]( <P> <B>)13 * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
And one other small error. Your going to kick yourself for it. :)

You setup a for loop that goes from 1 to 2. This means the loop will execute once for 1 and a second time for 2.
The second time through will always evaluate false because there is no second checkbox so it will return true because of how your function is setup.

Drop the loop number to 1 or remove the loop entirely if there is only one box to test and it works.



Google, you're my hero!
 
thanks for the quick reply.

I was trying to avoid using onSubmit so I did not use the first option. Thank you cLFlaVA for the help though.
Thank you "theniteowl" for the reply. The loop correction worked good.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top