Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When writing HTML source code, you can define <em>attributes</em> on your HTML elements. Then, once the browser parses your code, a corresponding DOM node will be created. This node is an object, and therefore it has <em>properties</em>.</p> <p>For instance, this HTML element:</p> <pre><code>&lt;input type="text" value="Name:"&gt; </code></pre> <p>has 2 attributes (<code>type</code> and <code>value</code>).</p> <p>Once the browser parses this code, a <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement" rel="noreferrer">HTMLInputElement</a> object will be created, and this object will contain dozens of properties like: accept, accessKey, align, alt, attributes, autofocus, baseURI, checked, childElementCount, childNodes, children, classList, className, clientHeight, etc.</p> <p>For a given DOM node object, properties are the properties of that object, and attributes are the elements of the <code>attributes</code> property of that object.</p> <p>When a DOM node is created for a given HTML element, many of its properties relate to attributes with the same or similar names, but it's not a one-to-one relationship. For instance, for this HTML element:</p> <pre><code>&lt;input id="the-input" type="text" value="Name:"&gt; </code></pre> <p>the corresponding DOM node will have <code>id</code>,<code>type</code>, and <code>value</code> properties (among others):</p> <ul> <li><p>The <code>id</code> property is a <em>reflected property</em> for the <code>id</code> attribute: Getting the property reads the attribute value, and setting the property writes the attribute value. <code>id</code> is a <em>pure</em> reflected property, it doesn't modify or limit the value.</p></li> <li><p>The <code>type</code> property is a <em>reflected property</em> for the <code>type</code> attribute: Getting the property reads the attribute value, and setting the property writes the attribute value. <code>type</code> isn't a pure reflected property because it's limited to <em>known values</em> (e.g., the valid types of an input). If you had <code>&lt;input type="foo"&gt;</code>, then <code>theInput.getAttribute("type")</code> gives you <code>"foo"</code> but <code>theInput.type</code> gives you <code>"text"</code>.</p></li> <li><p>In contrast, the <code>value</code> property doesn't reflect the <code>value</code> attribute. Instead, it's the <em>current value</em> of the input. When the user manually changes the value of the input box, the <code>value</code> property will reflect this change. So if the user inputs <code>"John"</code> into the input box, then:</p> <pre><code>theInput.value // returns "John" </code></pre> <p>whereas:</p> <pre><code>theInput.getAttribute('value') // returns "Name:" </code></pre> <p>The <code>value</code> property reflects the <strong>current</strong> text-content inside the input box, whereas the <code>value</code> attribute contains the <strong>initial</strong> text-content of the <code>value</code> attribute from the HTML source code.</p> <p>So if you want to know what's currently inside the text-box, read the property. If you, however, want to know what the initial value of the text-box was, read the attribute. Or you can use the <code>defaultValue</code> property, which is a pure reflection of the <code>value</code> attribute:</p> <pre><code>theInput.value // returns "John" theInput.getAttribute('value') // returns "Name:" theInput.defaultValue // returns "Name:" </code></pre></li> </ul> <p>There are several properties that directly reflect their attribute (<code>rel</code>, <code>id</code>), some are direct reflections with slightly-different names (<code>htmlFor</code> reflects the <code>for</code> attribute, <code>className</code> reflects the <code>class</code> attribute), many that reflect their attribute but with restrictions/modifications (<code>src</code>, <code>href</code>, <code>disabled</code>, <code>multiple</code>), and so on. <a href="https://www.w3.org/TR/html5/infrastructure.html#reflect" rel="noreferrer">The spec</a> covers the various kinds of reflection.</p>
    singulars
    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