javascript - Is selected option selected if attribute is in DOM or not -
to create dual listbox bootstraps needed remove default gray color of selected
option. way remove gray color change selected state of element false. no other trick worked remove gray background color of selected option.
now if so, attribute selected
still remains in dom seems false. thought if have element like:
<option value="1" selected>1: lorem ipsum</option>
if selected
there means option selected true same as:
<option value="1" selected="selected">1: lorem ipsum</option>
or
<option value="1" selected="true">1: lorem ipsum</option>
but seems not so. shed light on me?
i've created fiddle https://jsfiddle.net/npm6tn0m/ trying demonstrate scenario. css
option[selected] { background-color: orange; }
only works selected
option set false still has selected
attribute in dom. cause may vary across different browsers here image see in osx chrome browser:
all code in fiddle here on so:
var elements = document.queryselectorall('.special option'); for(var i=0; < elements.length; i++) { elements[i].selected = false; } settimeout(function(){ var elements = document.queryselectorall('option'); for(var i=0; < elements.length; i++) { console.log('value = ' + elements[i].value + ' selected: '+ elements[i].selected); } }, 1000);
option[selected] { background-color: orange; }
<select class="special" multiple> <option value="1" selected>1: lorem ipsum</option> <option value="2" >2: dolor sit amet</option> </select> <select multiple> <option value="3" selected>3: lorem ipsum</option> <option value="4" >4: dolor sit amet</option> </select>
i thought if have element like:
<option value="1" selected>1: lorem ipsum</option>
if selected there means option selected true same as:
<option value="1" selected="selected">1: lorem ipsum</option>
...
but seems not so. shed light on me?
you're correct how boolean attributes work, not selected
boolean attribute represents. :-)
the selected
attribute represents default selected state, not current selected state. it's input
's value
attribute in way; default (used in case of form reset, instance), not current value.
just current value of input
is not represented in dom, current value of option
's selectedness not represented in dom.
Comments
Post a Comment