Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set an option value as selected

Tags:

html

jquery

I would like to load a select box where in the user's selected value will automatically appear.

I am receiving a Json data from the server with the user info. sample of data is: {"color":"red"}

In my html code I have select option like this:

<select id="input_user" class="selectinput" disabled="true">
<option value="n/a"> n/a </option>
<option value="red"> red </option> 
<option value="green"> green </option>
<option value="yellow"> yellow </option>
<option value="blue"> blue </option>
<option value="white"> white </option>
</select> //this will only show "n/a" as default

I tried this code to make the red value as default but not working.

var user_color= data[2].color; // this was from the json data
document.getElementById('input_user').selectedIndex = user_color;

Any help?

like image 518
c.k Avatar asked Jul 21 '16 07:07

c.k


People also ask

How do you set the value of a select option?

The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean attribute.

How do I get a dropdown selected value?

The value of the selected element can be found by using the value property on the selected element that defines the list. This property returns a string representing the value attribute of the <option> element in the list. If no option is selected then nothing will be returned.

How do I get the text value of a selected option?

find(":selected"). text(); it returns the text of the selected option.

What is selected in option tag?

The selected attribute is a boolean attribute. When present, it specifies that an option should be pre-selected when the page loads. The pre-selected option will be displayed first in the drop-down list. Tip: The selected attribute can also be set after the page loads, with a JavaScript.


2 Answers

As you've tagged this question with jQuery you can just set the val() on the select directly:

var user_color = 'red'; //data[2].color;
$('#input_user').val(user_color);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="input_user" class="selectinput" disabled="true">
  <option value="n/a">n/a</option>
  <option value="red">red</option>
  <option value="green">green</option>
  <option value="yellow">yellow</option>
  <option value="blue">blue</option>
  <option value="white">white</option>
</select>
like image 140
Rory McCrossan Avatar answered Sep 25 '22 07:09

Rory McCrossan


after much research, I finally went back to see the jQuery documentation that gave me the right answer :

1 / Delete all the attributes of tags that are already with the 'selected' attribute

function remove_selected(){
  $("select option").removeAttr('selected');
}

2 / I use the prop() function of jQuery to assign the 'selected' attribute to the desired tag

// USE THE FUNCTION TO REMOVE ALL ATTRIBUTES ALREADY SELECTED  
remove_selected();

// Set the tag <option> attribute to 'selected'
$('select option[value="ThatYouWant"]').prop({defaultSelected: true});

In HTML this gives :

<select>
  <option value="something">Something</option>
  <option value="ThatYouWant" selected>That you want</option>
  <option value="OtherValue">Other value</option>
</select>

It is clean and without burrs, thank you for your feedback and corrections, approve if you find this solution correct. Sincerely, Raf.

Source : https://api.jquery.com/prop/

like image 28
SNS - Web et Informatique Avatar answered Sep 21 '22 07:09

SNS - Web et Informatique