SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot thetzfreak's Avatar
    Join Date
    Aug 2004
    Location
    United States
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    OnClick For a Select Box

    Hi,

    I'd like to have a select box redirect to a different page when an option is chosen, but I'd only like to have it redirect for specified options. Most options will not do anything on click, but I'd like a couple to redirect on click.

    How exactly do I do this?

    Thanks.

  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Maybe set onchange to the options, rather than the select.

  3. #3
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by thetzfreak View Post
    Hi,

    I'd like to have a select box redirect to a different page when an option is chosen, but I'd only like to have it redirect for specified options. Most options will not do anything on click, but I'd like a couple to redirect on click.

    How exactly do I do this?

    Thanks.
    If you use onclick, the keyboard has no effect. You can redirect based on the selectedIndex or value properties. Search.
    Tab-indentation is a crime against humanity.

  4. #4
    SitePoint Zealot thetzfreak's Avatar
    Join Date
    Aug 2004
    Location
    United States
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, thanks for the help, but I still can't quite figure it out. Could you help me? I also tried wonshikee's suggestion, but it didn't work.

    How do I make it that it'll only redirect to a certain page if the value is equal to "redirect" or something similar?

    Thanks for the help.

  5. #5
    SitePoint Zealot thetzfreak's Avatar
    Join Date
    Aug 2004
    Location
    United States
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can someone please try helping me with this?

  6. #6
    SitePoint Zealot thetzfreak's Avatar
    Join Date
    Aug 2004
    Location
    United States
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there even a possible way to do this?

  7. #7
    SitePoint Zealot thetzfreak's Avatar
    Join Date
    Aug 2004
    Location
    United States
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll pay someone $5 or $10 to show me the code that does this.

    Thanks.

  8. #8
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    IE doesn't recognize onclick of <option>s so you should stick with onchange of the select.

    Here's one example that looks for "redirectTo:" in the value of the selected option, and if found go ahead and redirect.

    Code:
    <script type="text/javascript">
    function redirect(myselect) {
      if (myselect.value.indexOf("redirectTo:") == 0) {
        window.location.href = myselect.value.replace("redirectTo:", "");
      } else {
        //don't redirect
      }
    } 
    </script>
    
    
    <select onchange="redirect(this);">
      <option value="">Pick something</option>
      <option value="redirectTo:http://www.google.com">Go to Google</option>
      <option value="redirectTo:http://www.sitepoint.com">Go to Sitepoint</option>
      <option value="dosomethingelse">Don't redirect</option>
    </select>


Bookmarks

Posting Permissions

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