Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the 'selected option' of a select dropdown list with jquery

I have the following jquery function:

$.post('GetSalesRepfromCustomer', {     data: selectedObj.value }, function (result) {     alert(result[0]);     $('select[name^="salesrep"]').val(result[0]); }); 

result[0] is a value that I want to set as the selected item in my select box. result[0] equals Bruce jones.

the select box is populated by a database query but one of the rendered html is:

<select id="salesrep" data-theme="a" data-mini="true" name="salesrep"> <option value=""> </option> <option value="john smith">john smith</option> <option value="Bruce Jones">Bruce Jones</option> <option value="Adam Calitz">Adam Calitz</option> <option>108</option> </select> 

$('select[name^="salesrep"]').val(result[0]); doesn't populate the select box selected option. I have also tried $("#salesrep").val(result[0]); without any luck.

Any help appreciated.

so what I want is the selected / highlighted option in the salesrep dropdown list to be Bruce Jones.

HTML to be:

<select id="salesrep" data-theme="a" data-mini="true" name="salesrep"> <option value=""> </option> <option value="john smith">john smith</option> <option value="Bruce Jones" selected >Bruce Jones</option> <option value="Adam Calitz">Adam Calitz</option> <option>108</option> </select> 

Thanks again,

Entire Script

<script type="text/javascript">      $(document).ready(function () {             $("#customer").autocomplete(       {       source: "get_customers",       messages:       {       noResults: '',       results: function() {}       },       select: function( event, ui )       {        var selectedObj = ui.item;               $.post('GetSalesRepfromCustomer', {data:selectedObj.value},function(result) {        alert(result[0]);        var selnametest="Bruce Koller";     $("#salesrep").val(selnametest);      });        }       });          });   </script> 

Rendered HTML is:

<select id="salesrep" data-theme="a" data-mini="true" name="salesrep"> <option value=""> </option> <option value="RyanLubuschagne">Ryan Lubuschagne</option> <option value="Bruce Jones">Bruce Jones</option> <option value="Adam Calitz">Adam Calitz</option> </select> 
like image 286
Smudger Avatar asked Jul 25 '13 11:07

Smudger


People also ask

How can I set the value of a DropDownList using jQuery?

Use $('select[id="salesrep"]'). val() to retrieve the selected value. Use $('select[id="salesrep"]'). val("john smith") to select a value from dropdown.

How do I set a dropdown selected value?

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. Example 2 :- Set selected value of dropdown in jquery by Name.

How do you select a particular option in a select element in jQuery?

Syntax of jQuery Select Option$(“selector option: selected”); The jQuery select option is used to display selected content in the option tag. text syntax is below: var variableValue = $(“selector option: selected”).

How set multiple selected values in DropDownList jQuery?

With jQuery, you can use the . val() method to get an array of the selected values on a multi-select dropdown list.


1 Answers

Try this :

$('select[name^="salesrep"] option[value="Bruce Jones"]').attr("selected","selected"); 

Just replace option[value="Bruce Jones"] by option[value=result[0]]

And before selecting a new option, you might want to "unselect" the previous :

$('select[name^="salesrep"] option:selected').attr("selected",null); 

You may want to read this too : jQuery get specific option tag text

Edit: Using jQuery Mobile, this link may provide a good solution : jquery mobile - set select/option values

like image 111
Jb Drucker Avatar answered Sep 19 '22 11:09

Jb Drucker