0

I was at How to change the href for a hyperlink using jQuery and there was an suggestion

With jQuery 1.6 and above you should use:

$("a").prop("href", "http://www.jakcms.com")

with the reason pointing to .prop() vs .attr(), whose updated answer was:

Update 1 November 2012

My original answer applies specifically to jQuery 1.6. My advice remains the same but jQuery 1.6.1 changed things slightly: in the face of the predicted pile of broken websites, the jQuery team [reverted attr() to something close to (but not exactly the same as) its old behaviour for Boolean attributes][1]. John Resig also [blogged about it][2]. I can see the difficulty they were in but still disagree with his recommendation to prefer attr().

Please pardon my ignorance but having read it over and over, I'm still at lost which one to choose, as up to now, every site/place I'm seeing are still using attr(). E.g.,

https://www.w3resource.com/jquery-exercises/jquery-core-exercise-9.php

was Last update on February 26 2020 this year.

I.e., if .prop() is the way to go, wouldn't everyone be using it 8/9 years later?

So which one should I use? What are the pros and cons of them both?

xpt
  • 20,363
  • 37
  • 127
  • 216
  • 1
    You should *almost always* use `.prop()`, especially if you don't understand the subtle differences. `.attr()` is more-or-less a wrapper for `.setAttribute()`, while `.prop()` is a wrapper for updating the DOM node directly. Many, if not most, uses of `.attr()` in the wild are inappropriate. – Pointy Sep 19 '20 at 15:18
  • In particular, look at the answers to that question you referenced, in particular the answer from the T.J. Crowder. That question really contains all you could possibly need to know about the issue. – Pointy Sep 19 '20 at 15:24
  • Also the answer to the question about why "everyone" isn't doing the right thing is that most software is terrible. – Pointy Sep 19 '20 at 15:25

0 Answers0