how to pass the slected option from a html drop down list to a java script variable??
-
This is my drop down list..If i select Others here..i need to dispalya text box..and pass that text box value to a java script variabble
<select id="dept"> <option value="Dept1">Dept1</option> <option value="Dept2">Dept2</option> <option value="Dept3">Dept3</option> <option value="Others">Others</option> </select>
-
This is my drop down list..If i select Others here..i need to dispalya text box..and pass that text box value to a java script variabble
<select id="dept"> <option value="Dept1">Dept1</option> <option value="Dept2">Dept2</option> <option value="Dept3">Dept3</option> <option value="Others">Others</option> </select>
If what you want to get is the value attribute then:
function getValue(){
var e = document.getElementById("dept");
var strDept = e.options[e.selectedIndex].value;
}If you want to get the text of the selected value
function getText(){
var e = document.getElementById("dept");
var strDept= e.options[e.selectedIndex].text;
}Then you will need to hook the function into the onchange event of your ...
-
This is my drop down list..If i select Others here..i need to dispalya text box..and pass that text box value to a java script variabble
<select id="dept"> <option value="Dept1">Dept1</option> <option value="Dept2">Dept2</option> <option value="Dept3">Dept3</option> <option value="Others">Others</option> </select>
Your select will need a onchange event to call a javascript function that will test the value of the selected option. If it turns out to be equal to "Others" then you can either, add the need html dynamically or just make it visible by using the css display property. javascript
function test_selection()
{
var sel = document.getElementById('dept');
var val = sel.options[sel.selectedIndex].value;
if(val=="Others")
{
....you can figure the rest from here.
}
}html
<select id="dept" onchange="test_selection();">
<option value="Dept1">Dept1</option>
<option value="Dept2">Dept2</option>
<option value="Dept3">Dept3</option>
<option value="Others">Others</option>
</select> -
If what you want to get is the value attribute then:
function getValue(){
var e = document.getElementById("dept");
var strDept = e.options[e.selectedIndex].value;
}If you want to get the text of the selected value
function getText(){
var e = document.getElementById("dept");
var strDept= e.options[e.selectedIndex].text;
}Then you will need to hook the function into the onchange event of your ...
-
-
Thanx a lot Jules..
-
Your select will need a onchange event to call a javascript function that will test the value of the selected option. If it turns out to be equal to "Others" then you can either, add the need html dynamically or just make it visible by using the css display property. javascript
function test_selection()
{
var sel = document.getElementById('dept');
var val = sel.options[sel.selectedIndex].value;
if(val=="Others")
{
....you can figure the rest from here.
}
}html
<select id="dept" onchange="test_selection();">
<option value="Dept1">Dept1</option>
<option value="Dept2">Dept2</option>
<option value="Dept3">Dept3</option>
<option value="Others">Others</option>
</select>Thanx a lot..its working..