Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to get value from select tag in variabel.

i have created an div in my aspx page
HTML
<div id="myDivResult"></div>


In this in my javascript i create an select tag with option
Like this
JavaScript
var selectList = document.createElement("select");
var myDiv = document.getElementById("myDivResult");
     while (myDiv.hasChildNodes()) {
         myDiv.removeChild(myDiv.firstChild);

     }

JavaScript
myDiv.appendChild(selectList);

I also have created option from an array with some item to add to the select tag.
Like this

JavaScript
var free = "";
            for (var j = 0; j < bookedFreeTimes.length; j++) {

                free = bookedFreeTimes[j];
                var array = new Array(free);

                for (var k = 0; k < array.length; k++) {
                    
                    var option = document.createElement("option");
                    option.text = array[k];
                    selectList.appendChild(option);

                }
            }


My problem is next how could i get value from my select tag in my next function

JavaScript
function getItem()
{
 var listItemFromAndToDate = document.getElementById('myDivResult').value;
}


I have tried like this but the value gets undenified?
What should i use here instead to get the value from the select tag?
Posted
Updated 5-Dec-14 10:47am
v2

1 solution

check this sample code : Demo[^]
JavaScript
$('#button').click(function () {
    var selectList = document.createElement("select");
    var myDiv = document.getElementById("myDivResult");
    while (myDiv.hasChildNodes()) {
        myDiv.removeChild(myDiv.firstChild);
    }
    myDiv.appendChild(selectList);
    for (var k = 0; k < 3; k++) {
        var option = document.createElement("option");
        option.text = k;
        selectList.appendChild(option);

    }
});

$('#button2').click(function () {
    alert("you have selected :" + $('#myDivResult').find('select').val());
})
;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900