SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Mar 2002
    Location
    Los Angeles
    Posts
    325
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    is it possible to grab the label of a SELECT menu?

    I was wondering if it was possible to grab the label of a SELECT drop down menu, in addition to the value...like in this case:

    HTML Code:
       <select name="cat">
       <option value=1>This is option 1
      </select>
    i would want to grab both the value, "1", and the label, "This is option 1", to send off to a script.
    From here on, it's instinctual...even straight roads meander.

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    You can grab those values in Javascript:
    Code:
    var theSelect = document.forms[0].elements['cat'];
    var selectedOptionText = theSelect.options[theSelect.selectedIndex].text;
    var selectedOptionValue = theSelect.options[theSelect.selectedIndex].value;
    In my example above, selectedOptionText grabs the text part and selectedOptionValue grabs the value part of the selected option.

  3. #3
    SitePoint Addict
    Join Date
    Mar 2002
    Location
    Los Angeles
    Posts
    325
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    perfect..thanks
    From here on, it's instinctual...even straight roads meander.

  4. #4
    SitePoint Addict
    Join Date
    Mar 2002
    Location
    Los Angeles
    Posts
    325
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how do I actually add the vars to the form being submitted?

    Code:
    <script language="JavaScript1.2" type="text/JavaScript1.2">
    function getFormValues(form){
      var theSelect = form.elements['cat'];
      var selectedText = theSelect.options[theSelect.selectedIndex].text;
      var selectedValue = theSelect.options[theSelect.selectedIndex].value;
    
    
      var query = form.elements['query'].value;
    
      form.submit();
    }
    </script>
    and on the button for the form i have:
    Code:
    <input type="button" name="Button" value="Search" onClick="getFormValues(this.form)">
    From here on, it's instinctual...even straight roads meander.


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
  •