jQuery cannot get custom attributes on UL elements
Question
I am currently focussing the following issue with jQuery 1.8.2.
Custom attributes are working fine for e.g. input fields:
<input id="test" required="true">
$("#test").prop("required")
true
$("#test").get(0).required
true
But not with UL elements.
<ul id="test" required="true"></ul>
$("#test").prop("required")
undefined
$("#test").get(0).required
undefined
Any ideas why it does not work with UL elements in Firefox 16 but in MSIE 8? Thanks!
Solution
If you need to use custom attributes for one reason or another, check out HTML5 data-
attributes.
<ul id="test" data-required="true"></ul>
console.log($('#test').data('required'));
OTHER TIPS
That's because HTML5 required
is a valid property for input elements and jQuery returns the value of this property, and for an ul
element required
is just an invalid attribute. An ul
element has no required
property, so it's undefined.
You must use:
$('#test').attr('required')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow