#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    3
    Rep Power
    0

    Javascript - onChange in only some options?


    Working on an existing form, there's one dropdown list of US States that I need to change. The request is to make some (most) of the choices jump to a different webpage. No problem, just use onChange. The other States, however, if selected, need to just pass their values through the form as they normally would.

    Here's the form:
    https://secure.myloanquote.com/applynow.asp

    So basically, this is an example of what needs to happen. Imagine this as the dropdown, in parenthesis what needs to happen if that option is chosen:

    AL (Pass value of AL through form)
    AK (Pass value of AK through form)
    AZ (onChange jump to http://www.yahoo.com)
    CA (onChange jump to http://www.yahoo.com)
    CO (Pass value of CO through form)

    Hope that's fairly clear. I'm thinking

    a) Is there any way to "turn off" onChange for specific selections in a dropdown?

    b) Would you do this with an and'if statement? If so, how do you write it (I'm not good with javascript).

    Thanks in advance,

    -z
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    May 2003
    Posts
    1,014
    Rep Power
    15
    If the page to jump to is always the same page, and if you don't need the values for the jump options anymore:
    -change the values for the jump options to ' ' (it's one space; the value of 'Choose State' is an empty string and it should be different from that)
    -and define an onchange on the select element:
    Code:
    <select id="myselect" onchange="if (this.value == ' ') location.href = 'http://www.yahoo.com'">
    If you don't/can't change the option values for the jump options,
    or if the pages to jump to vary for different states,
    I'd keep the values of the options intact and set up a separate function:
    Code:
    function checkSelectedOption(value) {
    	switch (value) {
    		case 'AZ' : location.href = 'http://www.cnn.com'; break;
    		case 'CA' : location.href = 'http://www.yahoo.com'; break;
    	}
    }
    <select id="myselect" onchange="checkSelectedOption(this.value)">
    Hope this helps,
    Jeroen
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    3
    Rep Power
    0
    Jeroen,

    That's EXACTLY what I need. You're awesome.

    -z

IMN logo majestic logo threadwatch logo seochat tools logo