3

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!

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Peter
  • 41
  • 5

3 Answers3

4

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

billyonecan
  • 20,090
  • 8
  • 42
  • 64
1

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

Ram
  • 143,282
  • 16
  • 168
  • 197
-1

You must use:

$('#test').attr('required')
voodmania
  • 73
  • 8