Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change DropDownList Selected value in Javascript?

Tags:

javascript

how to change the selected value in javascript and get the selected value in codebehind page? AutoPostBack is set to false.

like image 582
karthik Avatar asked Apr 05 '12 13:04

karthik


People also ask

How do you set the value of select?

Use the value property to set the value of a select element, e.g. select. value = 'new value' . The value property can be used to set or update the value of a select element.

How can I change the selected value of a DropDownList using jQuery?

Using the jQuery change() method; you can set the selected value of dropdown in jquery by using id, name, class, and tag with selected html elements; see the following example for that: Example 1 :- Set selected value of dropdown in jquery by id.


1 Answers

You can change it like this:

var ddl = document.getElementById('ddl-id');
var opts = ddl.options.length;
for (var i=0; i<opts; i++){
    if (ddl.options[i].value == "some-value"){
        ddl.options[i].selected = true;
        break;
    }
}
like image 138
Coder Avatar answered Oct 22 '22 13:10

Coder