Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add "selected" in option attribute using Javascript or jQuery?

Below is code for select option and generate using php from database and i try to add selected="selected" to value="4" using jQuery or any javascript:

<select id="countryselect" name="country">
<option value="1">Afghanistan</option>
<option value="2">Albania</option>
<option value="3">Algeria</option>
<option value="4">Malaysia</option>
<option value="5">Maldives</option>
</select>

i try to refer this post but still can't .. below is my current script :

<script>
localStorage.setItem("Select1", "Malaysia");
    $('#countryselect').find('option').each(function(i,e){
        if($(e).val() == localStorage.getItem("Select1")){
            $('#countryselect').prop('selectedIndex',i);
        }
    });
</script>

thanks.

like image 744
rusly Avatar asked Sep 24 '12 02:09

rusly


People also ask

How add data attribute to select option?

HTML <option> data-* AttributeA data-* attribute on an <option> tag attaches additional data to the option item. To create a custom attribute, replace * with a lowercase string, such as data-id , data-status , or data-location .

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 would you add elements to selection using jQuery?

Method 1: Append the option tag to the select box The select box is selected with the jQuery selector and this option is added with the append() method. The append() method inserts the specified content as the last child of the jQuery collection. Hence the option is added to the select element.

How do I set selected options in HTML?

The select tag in HTML is used to create a dropdown list of options that can be selected. The option tag contains the value that would be used when selected. The default value of the select element can be set by using the 'selected' attribute on the required option.


2 Answers

This should work simple, $("#countryselect").val('4')​

It will automatically set selected=selected

Demo: http://jsfiddle.net/muthkum/zYRkC/

like image 74
Muthu Kumaran Avatar answered Oct 22 '22 05:10

Muthu Kumaran


The selected attribute is a boolean attribute, its presence sets the value of the related DOM property to true. If the attribute is absent, the value of the selected property is false.

If an option has the selected attribute, then when the page is first loaded, or the form the control is in is reset, that option will be the selected option.

If the option's selected property is set to true, then that option will be selected. However, if the form is reset, the default selected option will be selected (i.e. the one with the selected attribute, or the first option, or none).

To set the selected attribute (i.e. make the option the default selected option):

var select = document.getElementById('countryselect');
var option;

for (var i=0; i<select.options.length; i++) {
  option = select.options[i];

  if (option.value == '4') {
  // or
  // if (option.text == 'Malaysia') {
     option.setAttribute('selected', true);

     // For a single select, the job's done
     return; 
  } 
}

Note that this may not make the option the currently selected option, it will just add the selected attribute. To make sure it's selected (if that is what is required), the also set the selected property to true (see below).

Note that the second argument to setAttribute is supposed to be a string that is used to set the attribute's value. However, the selected attribute doesn't have a "setable" value, so the second argument is ignored (e.g. even false will still set the attribute and make the option the default selected option). That causes some confusion. :-)

To set the selected property (i.e. just make the option the current selected option):

var select = document.getElementById('countryselect');
var option;

for (var i=0; i<select.options.length; i++) {
  option = select.options[i];

  if (option.value == '4') {
  // or
  // if (option.text == 'Malaysia') {
     option.selected = true;
     return;
  } 
}
like image 40
RobG Avatar answered Oct 22 '22 04:10

RobG