views:

34250

answers:

5

how to get selected value from dropdownlist in asp.net using javascript?

i have tried the methods below but each of them only return the selected index instead of the value:

var as = document.form1.ddlViewBy.value;

var e = document.getElementById("ddlViewBy");
var strUser = e.options[e.selectedIndex].value;
+2  A: 

When in doubt, open up Firebug, enable its Console (tab), and start typing in Javascript.

What Firebug spits back at you will be the values of expressions you put in. This process will teach you way more than you can imagine.

Artem Russakovskii
You can do the same thing in Chrome (Developer>Javascript Console) and IE8 (Developer Tools>Script>Console)
Keith
+8  A: 

If you have a select element that looks like this:

<select id="ddlViewBy">
<option value="1">test1</option>
<option value="2" selected="selected">test2</option>
<option value="3">test3</option>
</select>

Running this code:

var e = document.getElementById("ddlViewBy");
var strUser = e.options[e.selectedIndex].value;

Would make strUser be 2. If what you actually want is test2, then do this:

var e = document.getElementById("ddlViewBy");
var strUser = e.options[e.selectedIndex].text;

Which would make strUser be test2

Paolo Bergantino
This is what i want! Thank you so much!
WeeShian
+5  A: 
var strUser = e.options[e.selectedIndex].value;

This is correct and should give you the value. Is it the text you're after?

var strUser = e.options[e.selectedIndex].text;

So you're clear on the terminology:

<select>
    <option value="hello">Hello World</option>
</select>

This option has:

  • Index = 0
  • Value = hello
  • Text = Hello World
Greg
i thought the ".value" in javascript should returns the value for me but only the ".text" would returns as what the .SelectedValue in asp.net returns. Thanks for example given!
WeeShian
A: 

The only reason I can see that makes this code not work is if you're using IE7-, and forgot to specify the value attribute for your <option>-tags... Every other browser should convert what's inside open-close tags as the option value.

peirix
A: 

function validate() { var e = document.getElementById("dd_date"); var dd = e.options[e.selectedIndex].value; var d = document.getElementById("dd_month"); var mm = d.options[d.selectedIndex].value; var i = document.getElementById("dd_year"); var yy = i.options[i.selectedIndex].value; var cal=dd+"-"+mm+"-"+yy; alert("The DOB is"+cal); }

document.write("dd:");

document.write("<select id=dd_date >");
var date;

for(date=1;date<=31;date++){

document.write("<option>" + date + "</option>");

} document.write(""); document.write("month:");

  document.write("<select id=dd_month>");
  var month;
  for(month=1;month<=12;month++){

document.write("<option>" + month + "</option>");

} document.write(""); document.write("year:");

  document.write("<select id=dd_year>");
  var year;
  for(year=1980;year<=2009;year++){

document.write("<option>" + year + "</option>");

} document.write("");

</script>
  <body>
   <form name=form1 action="" method="post">
   <input type="submit" value="Submit" onClick="validate();">