Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    May 2005
    Posts
    190
    Thanks
    1
    Thanked 2 Times in 2 Posts

    how do i pull what is in between the option tag

    in a dropdown box, i know that i can do ele.value to get the value of the option, but i want the label or 'xxx'

    <option val='val'>xxx</option>

    thanks

  • #2
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Use ele.text.
    dumpfi
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.

  • #3
    Regular Coder
    Join Date
    May 2005
    Posts
    190
    Thanks
    1
    Thanked 2 Times in 2 Posts
    that returns undefined

    document.getElementById('course_id').text //course_id is the dropdown

  • #4
    Senior Coder coothead's Avatar
    Join Date
    Jan 2004
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,703
    Thanks
    0
    Thanked 233 Times in 228 Posts
    Hi there emehrkay,

    here is an example...
    Code:
    <script type="text/javascript">
    <!--
    window.onload=function(){
        df=document.forms[0][0];
    for(c=0;c<df.options.length;c++) {
        alert(df[c].firstChild.data);
      }
     }
    //-->
    </script>
    
    <form action="#">
    <div>
    <select>
    <option>---</option>
    <option>xxx</option>
    <option>yyy</option>
    <option>zzz</option>
    </select>
    </div>
    </form>
    coothead

  • #5
    Regular Coder
    Join Date
    May 2005
    Posts
    190
    Thanks
    1
    Thanked 2 Times in 2 Posts
    coothead that works, but how can i just pull the value that is selected?

  • #6
    Senior Coder coothead's Avatar
    Join Date
    Jan 2004
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,703
    Thanks
    0
    Thanked 233 Times in 228 Posts
    Hi there emehrkay,

    no problem...
    Code:
    <script type="text/javascript">
    <!--
    window.onload=function(){
        df=document.forms[0][0];
    df.onchange=function() {
        alert(df.options[df.options.selectedIndex].firstChild.data);
      }
     }
    //-->
    </script>
    
    <form action="#">
    <div>
    <select>
    <option value="0">---</option>
    <option value="1">xxx</option>
    <option value="2">yyy</option>
    <option value="3">zzz</option>
    </select>
    </div>
    </form>
    coothead

  • #7
    Regular Coder
    Join Date
    May 2005
    Posts
    190
    Thanks
    1
    Thanked 2 Times in 2 Posts
    thanks coothead, i got it beforre i checked the thread

    document.getElementById('course_id').childNodes[0].firstChild.nodeValue;

  • #8
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Quote Originally Posted by emehrkay
    document.getElementById('course_id').text //course_id is the dropdown
    I should have mentioned that this is a property of the option element and not the select element.

    So you would have to use something like:
    Code:
    var sel = document.getElementById('course_id');
    alert(sel.options[sel.options.selectedIndex].text);
    dumpfi
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.


  •  

    Posting Permissions

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