Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>selected</code> attribute does <strong>not</strong> correspond to the current selectedness state of the option. The <code>selected</code> attribute corresponds to the <em>default</em> selectedness, which will be restored if <code>.reset()</code> is called (or a <code>type="reset"</code> button is clicked) on the form. The current selectedness state can be accessed using the DOM property <code>selected</code>, whereas the default-selectedness, as reflected by the attribute, is accessed under the DOM property <code>defaultSelected</code>.</p> <p>The same goes for <code>value</code> vs <code>defaultValue</code> on <code>&lt;input type="text"&gt;</code>/<code>&lt;textarea&gt;</code>, and <code>checked</code>/<code>defaultChecked</code> on <code>type="checkbox"</code>/<code>"radio"</code>.</p> <p>jQuery's <code>attr()</code> is misleadingly named, and its attempts to pretend attributes and properties are the same thing is flawed. When you use <code>attr()</code>, you are usually accessing the property, <em>not</em> the attribute. Consequently, <code>$(el).attr('selected', 'selected')</code> is actually doing <code>el.selected= 'selected'</code>. This works because <code>'selected'</code>, as with any non-empty string, is ‘truthy’: it gets converted to <code>el.selected= true</code>. But this does not at any point touch the <code>selected="selected"</code> attribute.</p> <p>IE further complicates this already confusing situation by (a) getting <code>getAttribute</code>/<code>setAttribute</code> wrong so it accesses the properties instead of the attributes (which is why you should never use these methods in an HTML document), and (b) incorrectly mapping the current form state to the HTML attributes, so the attributes do actually appear in that browser.</p> <blockquote> <p>but I need selected="selected" there</p> </blockquote> <p>Why? Are you serialising the form to <code>innerHTML</code>? This won't include form field values (again, except in IE due to the bug), so is not a usable way to store field values.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload