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?
The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean attribute.
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.
find(":selected"). text(); it returns the text of the selected option.
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.
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>
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With