Right now I'm doing various invalid things like:
<span time="50" distance="60"></span>
And then grabbing that info with:
var time = $('span').attr('time');
var distance = $('span').attr('distance');
And then doing various things with the time and distance in jS/jQuery.
To me, this feels wrong. It's insemantic, but I really can't care less about that (actual data is not time and distance but something quite worthless and page specific, nothing that SEs are interested in). But is there some other reason why this is a bad idea?
I know there's a metadata plugin which does something similar in a more 'official' way, and I thought about using it. But this .attr stuff is suitable for my needs and I don't see any compelling reason to use the plugin.
So basically, is this a decent pattern to use, and if not, why not, and should I be interested in the metadata plugin.
Thanks.
The attr() method sets or returns attributes and values of the selected elements. When this method is used to return the attribute value, it returns the value of the FIRST matched element. When this method is used to set attribute values, it sets one or more attribute/value pairs for the set of matched elements.
Alternatively, you can also use the jQuery data() method (jQuery version >= 1.4. 3), to get the data-attribute of an element using the syntax like $(element). data(key) . That means in the above example to get the data-id using data() method you can use the statement like $(this).
jQuery deprecated (version 1.6) and removed (version 1.9) the use of . attr() for use to set "checked" and "disabled" properties. It also points out that attempting to retrieve values by using . attr() will result in confusion/problems, as in this example: $elem.
Attributes vs. As of jQuery 1.6, the . prop() method provides a way to explicitly retrieve property values, while . attr() retrieves attributes. For example, selectedIndex , tagName , nodeName , nodeType , ownerDocument , defaultChecked , and defaultSelected should be retrieved and set with the . prop() method.
HTML5 includes support for embedding data in attributes which is backwards compatible. Here's an example:
<li class="user" data-name="John Resig" data-city="Boston"
data-lang="js" data-food="Bacon">
<b>John says:</b> <span>Hello, how are you?</span>
</li>
Useragents will perhaps implement the .dataset idea into javascript, which would easily let you pull out the data bits seperately, but for now just changing your classes to include data- is good enough. This will validate as HTML5.
To me this is far better than that metadata jquery idea - that just seems dumb to me.
See here for more info.
If you are going to use custom attributes, and are using XHTML, it would be a good idea to namespace them and provide the appropriate 'xmlns' definition as part of the 'html' element. I do this any time I need to add attributes to provide some contextual information and it's worked quite well so far, especially with jQuery. You'd just use .attr('ns:name')
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With