SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Procrastinator Extraordinaire lucas's Avatar
    Join Date
    Oct 2000
    Location
    Springfield, Missouri, U.S.A.
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need to grab label, not id from list box

    In the following example code:
    Code:
    <select name="mylist" id="mylist">
    <option value="item 1">label 1</option>
    <option value="item 2">label 2</option>
    <option value="item 3">label 3</option>
    <option value="item 4">label 4</option>
    </select>
    I need to grab the value of the label instead of the value. I'm using a 3rd party service and their value is a unique id that is meaningless to me. Is there a way to do this in Javascript or HTML?

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,236
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Code:
    function alertActiveLabel(selectbox) {
      alert(selectbox.options[selectbox.selectedIndex].text);
    }
    HTML Code:
    <select name="mylist" id="mylist"
    onchange="alertActiveLabel(this);">
    <option value="item 1">label 1</option>
    <option value="item 2">label 2</option>
    <option value="item 3">label 3</option>
    <option value="item 4">label 4</option>
    </select>
    The .text property is the one you're after .

  3. #3
    Procrastinator Extraordinaire lucas's Avatar
    Join Date
    Oct 2000
    Location
    Springfield, Missouri, U.S.A.
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any idea how I can assign this to an ASP variable or pass it through a form as a hidden field?

  4. #4
    011521 dbalsdon's Avatar
    Join Date
    Feb 2003
    Location
    North Of Scotland
    Posts
    444
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
     Dim variable_name = Request.Form("myList")
    Daniel Balsdon
    My Site

  5. #5
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,236
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by lucas
    Any idea how I can assign this to an ASP variable or pass it through a form as a hidden field?
    This isn't the best solution accessibility-wise, but it's the best one I can think of right now unfortunately:
    Code:
    function moveToHidden(sBox, hidden) {
       hidden.value = sBox.options[sBox.selectedIndex].text;
       return true;
    }
    HTML Code:
    <form id="myform" 
    onsubmit="return moveToHidden(this.elements[]selectbox]], this.elements['hiddenfield']);">
      <select name="selectbox">
      <option value="blah">Item</option>
      </select>
      <input type="hidden" name="hiddenfield" />
    </form>

  6. #6
    Procrastinator Extraordinaire lucas's Avatar
    Join Date
    Oct 2000
    Location
    Springfield, Missouri, U.S.A.
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I could not get that code to work, but I ended up with something similar:

    Code:
    function changeText() 
    	{
    		var bulk = document.aform.ExpertKeys.options;
    		var old_text = bulk[bulk.selectedIndex].text;
    		document.aform.hdnRep.value = old_text;
    	}
    
    <form action="support_main.asp" method="post" name="aform" onsubmit="changeText()">
    
        <select name="ExpertKeys">
           <option value="1">1</option>
        </select>
    
        <input type="hidden" name="hdnRep">
    
    </form>
    On submit it grabs the text value from the select box and writes the value to a hidden field which can then be collected regularly by ASP.

    Thanks for pointing me in the right direction vgarcia.


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
  •