Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery cannot get custom attributes on UL elements

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!

like image 740
Peter Avatar asked May 14 '26 08:05

Peter


2 Answers

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'));

http://ejohn.org/blog/html-5-data-attributes/

http://api.jquery.com/data/#data-html5

like image 138
billyonecan Avatar answered May 16 '26 20:05

billyonecan


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.

http://www.whatwg.org/specs/web-apps/current-work/multipage/common-input-element-attributes.html#the-required-attribute

like image 22
undefined Avatar answered May 16 '26 22:05

undefined



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!