Please, explain how can I change 'selected' property of option? E.g.:
<select id="lang_select">
<option value="en" selected="selected">english</option>
<option value="ar">العربية</option>
<option value="az">azərbaycanlı</option>
<option value="bg">български</option>
<option value="ca">català</option>
<option value="cs">český</option>
<!-- some data cut -->
</select>
So, if I change the drop down list value nothing is changed in html-data. Why?
Only if I try to force reload the property using jQuery it works.
$(document).on('change',"select",function(){
var i = $(this)[0].selectedIndex;
var ch = $(this).children().each(function(index){
$(this).prop('selected',index == i);
if (index == i) {
$(this).attr('selected','selected');
} else {
$(this).removeAttr('selected');
}
});
});
Why? How can I avoid this? Is it possible to change "selected" using pure html?
EDIT I namely want this attrbute in html tag because I need to save and restore the part of this html-code in future.
To change the selected option in an HTML <select> element we have to change the value property on the <select> element or the selected property on the <option> element we want to get selected. There are different ways we can do it and choosing one or another will depend on the information we have available to us.
Essentially form:options tag gives you option to render a part of list you want to display in a drop down a whole of it. Having the form:select tag gives flexibility to have a combination of option and options tag inside it.
var select = $("<select></select>"); var opt = $("<option></option"); opt. val("1"); opt. html("Option 1"); select.
Updated, substituted .attr("selected", true)
for .prop("selected", true)
Note , value of selected
attribute returns true
or false
, not "selected"
.
Try utilizing selector $("option[value=" + this.value + "]", this)
, set property selected
to true
, .siblings()
to remove selected
attribute from option
not selected
$(document).on("change","select",function(){
$("option[value=" + this.value + "]", this)
.attr("selected", true).siblings()
.removeAttr("selected")
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select id="lang_select">
<option value="en" selected="true">english</option>
<option value="ar">العربية</option>
<option value="az">azərbaycanlı</option>
<option value="bg">български</option>
<option value="ca">català</option>
<option value="cs">český</option>
<!-- some data cut -->
</select>
It's not change the html
itself but it does change the value of the select
$('select').change(function(){
$('#result').html($(this).val());
}).trigger('change');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="lang_select">
<option value="en" selected="selected">english</option>
<option value="ar">العربية</option>
<option value="az">azərbaycanlı</option>
<option value="bg">български</option>
<option value="ca">català</option>
<option value="cs">český</option>
<!-- some data cut -->
</select>
<hr />
<div id="result"></div>
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