Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do i add a if statement to this?

    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

    Code:
    <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>

  • #2
    Regular Coder
    Join Date
    Aug 2008
    Posts
    104
    Thanks
    4
    Thanked 14 Times in 14 Posts
    Hi there,

    try this:

    Code:
    <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

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,243
    Thanks
    203
    Thanked 2,555 Times in 2,533 Posts
    Here you are:-


    Code:
    <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?

  • #4
    Regular Coder
    Join Date
    Aug 2008
    Posts
    104
    Thanks
    4
    Thanked 14 Times in 14 Posts
    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

  • #5
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Code:
    <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>

  • #6
    Regular Coder
    Join Date
    Aug 2008
    Posts
    104
    Thanks
    4
    Thanked 14 Times in 14 Posts
    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...

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,243
    Thanks
    203
    Thanked 2,555 Times in 2,533 Posts
    Quote Originally Posted by eddjc View Post
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •