www.webdeveloper.com
Results 1 to 3 of 3

Thread: Alert if no option selected.

Hybrid View

  1. #1
    Join Date
    Feb 2004
    Posts
    21

    Alert if no option selected.

    I'm trying to modify the following script...

    <form onsubmit="open(this.url.value,'','');return false;">
    <select name="url">
    <option value ="javascript:void(0)">Please select...</option>
    <option value ="http://www.yahoo.com">Yahoo</option>
    <option value ="http://www.google.com">Google</option>
    <option value ="http://www.apple.com">Apple</option>
    </select>
    <input type="submit" name="get" value="Go">
    </form>

    The script is working fine, but I want it to give an alert if they try to submit the form without choosing an option. In other words, select Yahoo and you go to Yahoo. But if you leave it on "Please select..." and press "go you get an alert saying "please select an option".

    Thanks for your help!

  2. #2
    Join Date
    Nov 2012
    Posts
    24
    Try it like this :
    Code:
    <form onsubmit="if(this.url.value=='noselected'){alert('Please select...');}else{open(this.url.value,'','');}return false;">
    <select name="url">
    <option value ="noselected">Please select...</option>
    <option value ="http://www.yahoo.com">Yahoo</option>
    <option value ="http://www.google.com">Google</option>
    <option value ="http://www.apple.com">Apple</option>
    </select>
    <input type="submit" name="get" value="Go">
    </form>
    It will check if the Please select... option is selected. If it is, it will alert, if it's not, it will redirect.
    Although I don't really like writing the javascripts within the listener in this case I did so it will more like your code but it would be better if you called a function onsubmit and have the javascript code within the function.

  3. #3
    Join Date
    Feb 2004
    Posts
    21
    This works great - thanks so much for your help!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles