SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Dec 2007
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Is there a way to get an option value's text?

    hopefully this is an easy one: 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?

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think the following code will help you to proceed.
    Code javascript:
    function ShowAll(frm) {
    	alert(frm.promolist.options[frm.promolist.selectedIndex].text);
    }
    HTML Code:
    <form name="frm1">
        <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><br>
    	<input type="button" name="btn1" id="btn1" value="Click Me" onClick="ShowAll(this.form);" />
    </form>
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use promolist[i].text


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
  •