Announcement

Collapse
No announcement yet.

Another javascript question

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Another javascript question

    Ok, hopefully this is a bit easier: I have a select control on my page, something like this:

    Code:
    <select name=promolist size=7 id=plist>
    <option value='000001'>Option 1</option>
    <option value='000002'>Option 2</option>
    <option value='000003'>Option 3</option>
    ....
    </select>
    And I came up with this little javascript to simply pop up an alert for the selected item:

    Code:
    function ShowAll() {
       len = document.promos.promolist.length
       i = 0
       chosen = "none"
    
       for (i = 0; i < len; i++) {
       if (document.promos.promolist[i].selected) {
          chosen = document.promos.promolist[i].value
         } 
         }
       alert(chosen);
       return false;
    }
    </script>
    There is a button in the form like this:
    <input type="normal" name="button1" onClick="ShowAll()">

    The only problem I have with this, is the alert value is not the text but the actual item value (du). So I'm wondering if anyone knows how to get the long text from the the select box instead of the item value?
    Software makes Hardware Happen

  • #2
    Save this url: http://www.w3schools.com/js/default.asp

    Your question:
    http://www.w3schools.com/htmldom/dom_obj_select.asp

    options[], selectedIndex

    Example:
    http://www.w3schools.com/htmldom/col...ct_options.asp
    Last edited by Edwin Knoppert; 31 Dec 2007, 02:52 AM.
    hellobasic

    Comment


    • #3
      Code:
      <select name="promolist" size="7" onchange="javascript:ShowAll(this)" id="plist">
          <option value='000001'>Option 1</option>
          <option value='000002'>Option 2</option>
          <option value='000003'>Option 3</option>
      </select>
      
      <script language="javascript" type="text/javascript">
      
      function ShowAll(plist)
      {
          alert( plist.options[plist.selectedIndex].text );
          return false;
      }
      </script>
      hellobasic

      Comment

      Working...
      X