...

View Full Version : Select box help?



Taipan
05-04-2007, 05:11 AM
Hi,

I want to stop my form from submitting if a select box value has not been chosen. When I use this code it works fine
<script language="JavaScript" type="text/JavaScript">
function checkForm( form1 ) {
if( !document.form1.select.options[document.form1.select1.selectedIndex].value ) {
var answer = window.alert( "You did not select an option." );
if( answer ) {
return true;
} else {
return false;
}
}
return true;
}
</script>


but my select boxes are an array and instead of being called "select1" it is called "option[1]". If I subbstitute "option[1]" for "select1" in the javascript it no longer works. Can anyone help me with this?

venkatmallu.net
05-04-2007, 10:27 AM
ur question is not clear...
plz give a clear picture of it once agian

whammy
05-04-2007, 06:09 PM
Can we see the rest of the form?

Taipan
05-04-2007, 10:27 PM
ur question is not clear...
plz give a clear picture of it once agian

Sorry, I will try to explain better. Part of my form is this:-


<form action="shop.php" name="form1" method="post">
<select id="option[1]" name="option[1]" class="select">
<option value="">SELECT - Colour</option>
<option value="7">Grey</option>
<option value="8">Red</option>
</select>
</form>

Note the name and id of the select. Now I want to stop the form being submitted if a Colour is not selected. My javascript is this:-


<script language="JavaScript" type="text/JavaScript">
function checkForm( form1 ) {

if( !document.form1.option[1].options[document.form1.option[1].selectedIndex].value ) {
var answer = window.alert( "You did not select an option." );
if( answer ) {
return true;
} else {
return false;
}
}
return true;
}
</script>

But that doesn't work. If I change the name of the select to something different like "select1" instead of "option[1]" in both lots of code it does work. But the select boxes are made using php and there may be more than one so they are named as an array. Any ideas how to make my javascript work using the existing select name?

I hope that explains my problem better.

glenngv
05-05-2007, 12:29 AM
document.form1.elements["option[1]"]
See Javascript Square Bracket notation in my sig for more info.

Taipan
05-05-2007, 12:43 AM
document.form1.elements["option[1]"]
See Javascript Square Bracket notation in my sig for more info.

Thanks ... perfect :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum