Note that there are some explanatory texts on larger screens.

plurals
  1. PORetrieving HTML attribute values "the DOM 0 way"
    primarykey
    data
    text
    <p>jQuery has an <code>attr()</code> method which retrieves the value of a given HTML attribute. For instance:</p> <pre><code>var foo = document.getElementById("foo"); $(foo).attr("id"); </code></pre> <p>But, performance-wise this is not optimal since an jQuery object has to be created just to call the <code>attr()</code> method. This performs better: <code>foo.id</code>.</p> <p>So, ideally we would want to avoid using <code>attr()</code>. But, can we do that (for any attribute)? I believe <code>foo.id</code> and <code>foo.value</code> is "safe" (cross-browser), but I remember having issues with <code>foo.href</code>.</p> <p>Here is a list of various attributes that I would like to be able to retrieve "directly":</p> <p>For any element: <code>foo.id</code>, <code>foo.name</code><br> For anchors: <strong><code>foo.href</code></strong>, <code>foo.target</code>, <code>foo.rel</code><br> For images, objects, iframes: <strong><code>foo.src</code></strong>, <code>foo.width</code>, <code>foo.height</code><br> For form elements: <code>foo.checked</code>, <strong><code>foo.selected</code></strong>, <code>foo.disabled</code>, <code>foo.readonly</code>, <code>foo.type</code>, <code>foo.value</code>, <code>foo.action</code></p> <p>So the question is: Are the above expressions cross-browser? Can I use them safely? </p> <p>A link to an article which examines this issue would also be nice.</p> <p><strong>Edit (based on the answers):</strong> The expressions in bold are not safe to use!</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.
 

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