SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Getting option values in a select

    I'm trying to do two different things depending on what is selected.

    1. Forward to a url based on a selected option
    2. Execute a function based on a selected option

    Can <option> tags have an onClick handler? My laptop is at home so trying to do this in my head prior to doing it.

    So:
    Code:
    <option onClick="sendValue(4);">Next Set</option>
    <option onClick="changeurl">Item Title</option>
    Am I going about it right? I tried something like onClick="window.location=the-url" but got errors. So not sure if I can do onClick or not or how you go about it.

  2. #2
    SitePoint Zealot
    Join Date
    Jul 2006
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    re: options

    What I do is use the onchange handler of the select itself, and find out which option was selected using something like:

    HTML:
    Code:
    <select id="mySelect" onchange="handle(this)">
      <option selected="selected"> </option>
      <option>A</option>
      <option>B</option>
    </select>
    JS:
    Code:
    function handle(el) {
        var o = el.selectIndex;
        if (!o) return;
        if (o == 1) {
            // do something
        }
        if (o == 2) {
            // do something
        }
    }
    I leave the first option empty so the user is encouraged to choose a different option thus setting off the onchange event.

    Dave

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply, just a few quick questions. The <select> is listing poems and the top and bottom options are previous and next links. If they select an actual poem, then they are forwarded to that url, if they select previous or next a function is run. So I can't use a general function to handle all choices. So will the onChange method still work?


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
  •