www.webdeveloper.com
Results 1 to 2 of 2

Thread: Getting text from a dynamically created combobox

  1. #1
    Join Date
    Jul 2012
    Location
    Srinagar Kashmir
    Posts
    1

    Exclamation Getting text from a dynamically created combobox

    i have created 2 comboboxes out of which one is dynamic in nature and data get into the 2nd combobox by selecting the data from first one. the problem is that i want get data from second combo box. i used a piece of code it works fine when used individually but when i merge it with my application code and call that code on onchange event of 2nd combobox in my application what is happenning i only get index number but not the text inside 2nd combobox , plz help me out.... code is pasted here...


    function getchanged1()
    {
    alert("working");
    var combo2value=document.getElementById("combo22").selectedIndex;
    alert(combo2value);

    var combo2text=combo2value.g;
    alert(combo2text);

    }
    ...............................calling from below combobox

    <select id="combo22" name="combo2" onchange="getchanged1()" style="width:218px; margin-left:2px;"
    <option>--Select a Subject--</option>
    <span class="Apple-converted-space">
    </span></select>

  2. #2
    Join Date
    Nov 2007
    Posts
    432
    .selectedIndex tells you the index of which item was selected, it does not give you the value. If you want the value, use .value.

    See here: http://www.jasondahlin.com/2011/codi...validation.asp

    The function in this page gives you the option of returning the text of the selected item, so you can ignore that part... here are the pieces you need:

    Code:
        var selectboxObj = document.getElementById(selectbox);
        if(!selectboxObj) { return "";}
    
        var selectedOption = selectboxObj.options[selectboxObj.selectedIndex];
        var selectedValue = selectedOption.value;
        return selectedValue;
    replacing "selectBox" with "combo22", you get:
    Code:
        var selectboxObj = document.getElementById('combo22');
        if(!selectboxObj) { return "";}
    
        var selectedOption = selectboxObj.options[selectboxObj.selectedIndex];
        var selectedValue = selectedOption.value;
        return selectedValue;

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles