Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select option is not updated 'selected' property in HTML web page

Tags:

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.

like image 733
Vyacheslav Avatar asked Aug 30 '15 14:08

Vyacheslav


People also ask

How do you change the selection options in HTML?

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.

What is difference between select and option in HTML?

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.

How do I create a dynamic selection in HTML?

var select = $("<select></select>"); var opt = $("<option></option"); opt. val("1"); opt. html("Option 1"); select.


2 Answers

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>
like image 167
guest271314 Avatar answered Nov 24 '22 10:11

guest271314


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>
like image 44
Mosh Feu Avatar answered Nov 24 '22 09:11

Mosh Feu