• 1
    @SviatKuzhelev The `selected` attribute controls only the default selected option, and *it does not change when a different option is selected by the user or via js*. To check that, you need to look at the `.selected` property of the DOM element, *not* the attribute. – CRice Feb 02 '18 at 00:05
  • 1
    The code 'as is' is working as expected on my machine. 'select.options[2].selected = true' is correctly causing the – Jorge González Feb 02 '18 at 00:05
  • 2 Answers2

    4

    You are confusing attributes and properties. Changing a property is a dynamic action that affects the in-memory object, it doesn't modify the static HTML that the element was originally parsed with. If you want to change the attribute, you need to use setAttribute() or removeAttribute().

    var select = document.body.querySelector('select');
    
    // Get the element with the "selected" attribute
    console.log("Element with 'selected' HTML attribute: " + 
       select.querySelector("[selected]").getAttribute("value"));
    
    // Get the element with the "selected" value:
    console.log("Element with value property of 'selected': " + select.value);
    
    // Change the value property of the select
    console.log("Changing value of select element to 'Rock'");
    select.value = "Rock";
    
    // Get the element with the "selected" attribute
    console.log("Element with 'selected' HTML attribute: " + 
       select.querySelector("[selected]").getAttribute("value"));  // Still Blues!
    
    // Get the element with the "selected" value:
    console.log("But, value of the select element is now: " + select.value);  // Now Rock!
    
    // Change which element has the selected attribute
    console.log("Switching elment that has the 'selected' attribute...");
    select.querySelector("[selected]").removeAttribute("selected");
    select.querySelector("[value=Rock]").setAttribute("selected", "selected");
    
    console.log("Element with 'selected' HTML attribute: " + 
      select.querySelector("[selected]").getAttribute("value"));
      
    console.log(select.options[0])
    console.log(select.options[1]);
    <select>
      <option value="Rock">Storm</option>
      <option value="Blues" selected>Rain</option>
    </select>
    Scott Marcus
    • 64,069
    • 6
    • 49
    • 71
    1

    To answer the question in the title, all you need to do is set the desired option as selected. To get current <select> value one should look into the select.value:

    let select = document.body.querySelector('select'),
      newOption = new Option('Classic', 'Classic');
    
    select.append(newOption);
    
    select.options[2].selected = true;
    
    console.log(select.value);
    // Classic
    <select>
      <option value="Rock">Storm</option>
      <option value="Blues" selected>Rain</option>
    </select>

    Also note selected attribute only marks default selection, and is different than the selected property of the <option>. The attribute simply tells the browser if it should render the option as selected when initially drawing the DOM element or not.


    When dealing with <select multiple>, you need to check the .selectedOptions property of the <select> and map it to an array. Example:

    let select = document.body.querySelector('select'),
      newOption = new Option('Classic', 'Classic');
    
    select.append(newOption);
    
    select.options[2].selected = true;
    
    let values = [].slice.call(select.selectedOptions).map(a => a.value);
    console.log(values)
    
    
    // ["Blues","Classic"]
    <select multiple>
      <option value="Rock">Storm</option>
      <option value="Blues" selected>Rain</option>
    </select>

    The .selectedOptions is an object which contains the selected options, the length and native methods, such as item() and namedItem(). Do note option groups are not contained.

    tao
    • 82,996
    • 16
    • 114
    • 150