...

View Full Version : How do i add a if statement to this?



djcritch
05-27-2009, 10:39 AM
hi need to put in a little if statement to my code so that if the value of the drop down = NA then it will display the following alert.


: alert('Sorry selection not available.'); break;

how do i go about adding this in to this piece of code as a if statement?


thanks guys



<html>
<head>
<title>Drop-down Selection </title>
<script type="text/javascript">
</script>
</head>
<body>
<form name="aform" id="aform">

option 1<input type="radio" name="footsize" value="three" onClick="document.aform.mymenu.options[0].selected=true">
option 2<input type="radio" name="footsize" value="four" onclick="document.aform.mymenu.options[1].selected=true">
option 3<input type="radio" name="footsize" value="five" onClick="document.aform.mymenu.options[2].selected=true">
option 4<input type="radio" name="footsize" value="six" onClick="document.aform.mymenu.options[3].selected=true">

<select id="mymenu" size="1">
<option value="nothing" selected="selected">Select a site</option>
<option value="http://www.dynamicdrive.com">Dynamic Drive</option>
<option value="na">na</option>
<option value="http://www.cssdrive.com">CSS Drive</option>
</select>

</form>
</body>
</html>

eddjc
05-27-2009, 11:31 AM
Hi there,

try this:


<select id="mymenu" size="1" onchange="if (this.options[this.selectedIndex].value == 'na') { alert('Sorry selection not available.'); this.selectedIndex = 0; }" >

<option value="nothing" selected="selected">Select a site</option>
<option value="http://www.dynamicdrive.com">Dynamic Drive</option>
<option value="na">na</option>
<option value="http://www.cssdrive.com">CSS Drive</option>
</select>

Edd

Philip M
05-27-2009, 11:40 AM
Here you are:-



<html>
<head>
<title>Drop-down Selection </title>
<script type="text/javascript">
</script>
</head>
<body>
<form name="aform" id="aform">

option 1<input type="radio" name="footsize" value="three" onClick="document.aform.mymenu.selectedIndex = 0">
option 2<input type="radio" name="footsize" value="four" onclick="document.aform.mymenu.selectedIndex = 1">
option 3<input type="radio" name="footsize" value="five" onClick="document.aform.mymenu.selectedIndex = 2; warn()">
option 4<input type="radio" name="footsize" value="six" onClick="document.aform.mymenu.selectedIndex = 3">

<select id="mymenu" size="1" onchange = "warn()">
<option value="nothing" selected="selected">Select a site</option>
<option value="http://www.dynamicdrive.com">Dynamic Drive</option>
<option value="na">na</option>
<option value="http://www.cssdrive.com">CSS Drive</option>
</select>


<script type = "text/javascript">
function warn() {
if (document.aform.mymenu.selectedIndex == 2) {
alert ('Sorry - that selection is not available.');
return false;
}
}

</script>
</form>
</body>
</html>


I'm not paranoid! Which of my enemies told you this?

eddjc
05-27-2009, 12:54 PM
Just as a matter of friendly debate - which do you think is better? Using the embedded functions in the html code (as I have), or having the script separate as Philip M has here?

Also - just my own solution, but wouldn't basing the if on value rather than index be better? Then without any extra coding you can have as many invalid selections as you like...


Cheers
Edd

djcritch
05-27-2009, 03:23 PM
ok so my code now looks like this and does just what i want it to do but . . .

it works fine in firefox but not ie plus ie is giving me an activex warning.



<html>
<head>
<title>Drop-down Selection </title>

</head>
<body>

<form name="aform" id="aform" onchange="if (mymenu.options[mymenu.selectedIndex].value == 'na') { alert('Sorry selection not available.'); mymenu.selectedIndex = 0; }">

option 1<input type="radio" name="footsize" value="three" onClick="document.aform.mymenu.options[0].selected=true">
option 2<input type="radio" name="footsize" value="four" onclick="document.aform.mymenu.options[1].selected=true">
option 3<input type="radio" name="footsize" value="five" onClick="document.aform.mymenu.options[2].selected=true">
option 4<input type="radio" name="footsize" value="six" onClick="document.aform.mymenu.options[3].selected=true">

<select id="mymenu" size="1" onchange="if (this.options[this.selectedIndex].value == 'na') { : alert('Sorry selection not available.'); this.selectedIndex = 0; }" >

<option value="nothing" selected="selected">Select a site</option>
<option value="http://www.dynamicdrive.com">Dynamic Drive</option>
<option value="na">na</option>
<option value="http://www.cssdrive.com">CSS Drive</option>

</select>
</form>
</body>
</html>

eddjc
05-27-2009, 04:28 PM
ah - that's where perhaps Philip M's idea might be better - I'm afraid I program on a mac so I can't check on IE - I know that IE handles JS in a very different way...

Try his suggestion and see if it works any better...

Philip M
05-27-2009, 06:02 PM
ah - that's where perhaps Philip M's idea might be better - I'm afraid I program on a mac so I can't check on IE - I know that IE handles JS in a very different way...

Try his suggestion and see if it works any better...

It works fine for me in IE6.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum