I know that this might be very simple question and I tried to find solutions in the web but I can't figure it... I have the following C# / ASPX code:
SelectArea += "<select name=\"Area\" id=\"area\">" + "<option value=\"Default\" style=\"display:none;\">SELECT AREA</option>";
for (i = 0; i < ds.Tables[0].Rows.Count; i++)
{
AreaName = ds.Tables[0].Rows[i]["AreaName"].ToString();
SelectArea += string.Format("<option value=\"{0}\"/>{0}</option>", AreaName);
}
SelectArea += "</select>";
And this javascript function that happeen after submit
function validateProfile() {
var el = document.getElementById("area").value;
alert(el);
}
I want some how to get the number of the selected value in the list. I tried with the code above but it doesn't work.
wish for help, thanks!
I know that this might be very simple question and I tried to find solutions in the web but I can't figure it... I have the following C# / ASPX code:
SelectArea += "<select name=\"Area\" id=\"area\">" + "<option value=\"Default\" style=\"display:none;\">SELECT AREA</option>";
for (i = 0; i < ds.Tables[0].Rows.Count; i++)
{
AreaName = ds.Tables[0].Rows[i]["AreaName"].ToString();
SelectArea += string.Format("<option value=\"{0}\"/>{0}</option>", AreaName);
}
SelectArea += "</select>";
And this javascript function that happeen after submit
function validateProfile() {
var el = document.getElementById("area").value;
alert(el);
}
I want some how to get the number of the selected value in the list. I tried with the code above but it doesn't work.
wish for help, thanks!
Share Improve this question asked May 23, 2013 at 16:39 Nave TsevaNave Tseva 8789 gold badges25 silver badges47 bronze badges 4- I'm just curious. Is there a reason not to use DropDownList Server Control? – Win Commented May 23, 2013 at 16:45
- Hi, Yes you right, it is better to use DropDownList Server Control but in this work there is specific request to do that like this. – Nave Tseva Commented May 23, 2013 at 16:48
-
Trivial but you could use single quotes:
"<select name='Area' id='area'><option value='Default' style='display:none;'>SELECT AREA</option>";
for that string – Mark Schultheiss Commented May 23, 2013 at 16:57 - Thanks, good advice (didn't know this befor ;), but someone knoes how to solve this?? – Nave Tseva Commented May 23, 2013 at 17:00
3 Answers
Reset to default 10For example if you have
<select id="myselect">
<option value="1">value1</option>
<option value="2" selected="selected">value2</option>
<option value="3">value3</option>
</select>
To get selected option do:
var selects = document.getElementById("myselect");
var selectedValue = selects.options[selects.selectedIndex].value;// will gives u 2
var selectedText = selects.options[selects.selectedIndex].text;// gives u value2
Sample JsFiddle
Try with this
var el = document.getElementById("area");
var selectedArea = el.options[el.selectedIndex].value;
Try :
var myList = document.getElementById("area");
var selectedValue = myList.options[myList.selectedIndex].value;
var selectedText = myList.options[myList.selectedIndex].text;