-2

I had some old jQuery working code with:

if ($element.attr('checked') === 'checked')

that was never rewriten to suggested:

if ($element.prop('checked'))

yet it worked.

But now, I've updated to jQuery 1.9.1 and accessing checked with attr returns undefined. Does it mean that jQuery dropped support for some DOM properties like checked/disabled/selected ?

Anujith
  • 9,370
  • 6
  • 33
  • 48
dragonfly
  • 17,407
  • 30
  • 110
  • 219
  • 2
    http://api.jquery.com/attr/ "As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. In addition, .attr() should not be used on plain objects, arrays, the window, or the document. To retrieve and change DOM properties, use the .prop() method." – jantimon Mar 05 '13 at 14:04
  • It was changed way back in [1.6](http://ejohn.org/blog/jquery-16-and-attr/), did you jump from 1.5? – epascarello Mar 05 '13 at 14:04
  • 1
    Have you tried reading the [jQuery documentation](http://api.jquery.com/attr/)? – Boris the Spider Mar 05 '13 at 14:04
  • When searching the web for examples, one is always bombarded with snippets using the "attr()" selector. Be nice. – Captain Kenpachi Mar 05 '13 at 14:10
  • No, I moved from 1.8.3 – dragonfly Mar 05 '13 at 14:13
  • I'm only suprised that support was droped now, when moving from 1.8.3 to 1.9.1. And actually you're all wrong. Up to 1.8.3 jQuery supported attr("checked"). Check this fiddle for 1.8.3 http://jsfiddle.net/ULEmr/ and this for 1.9.1 http://jsfiddle.net/nXVtb/1/ – dragonfly Mar 05 '13 at 14:21
  • I did not write that I use attr on my own - I can read docs and use prop. I just had a few legacy code written by other developers long time ago that simply worked and nobody bothered. Really uncanny, isn't it? – dragonfly Mar 05 '13 at 14:23

1 Answers1

1

jQuery 1.6 introduced the .prop() method for setting or getting properties on nodes and deprecated the use of .attr() to set properties. However, versions up to 1.9 continued to support using .attr() for specific situations. This behavior in the name of backwards compatibility causes confusion when selectors are used that distinguish between attributes and properties.

Have a look at this upgrade guide from jQuery itself. .attr() versus .prop()

Praveen Puglia
  • 5,577
  • 5
  • 34
  • 68