Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript AJAX Basics (retiring) AJAX and APIs Stage 4 Challenge Answer

Brendan Moran
Brendan Moran
14,052 Points

I understand the difference between the two, but why prop on the button and attr on the search field?

Just as the title says. I know that the prop is a property of the DOM, while the attributes are written directly into the HTML. Setting and getting can be done with both of them. Prop can be used on the search field just as well as attr. The weirdness is compounded by the fact that he is doing the same thing in both cases (setting disable to true). So that leads to the question, why does he quite intentionally use the two different jQuery methods? Is it just for teaching purposes, to show that they both exist, or is there a compelling reason to use attr on the search field instead of the more commonly recommended (from my research) prop?

3 Answers

http://jq4you.blogspot.com/2013/04/jquery-attr-vs-prop-difference.html

the above blog helped in understanding the difference. Hope it helps other's too!

Graham Tonelli
Graham Tonelli
11,968 Points

Here he uses the .val() method. If I'm not mistaken the .prop() method will return a boolean value in some cases, where the .attr() method will return a string. There might be other minor differences but this is the major one. Often times many methods are interchangeable depending on the arguments passed to them and the objective in mind.

Cheers, Graham

Michael Moore
Michael Moore
7,121 Points

This is just a wild guess. I am still very much learning this stuff. But I think the first one sets the property to disabled, and the second one is saying, if the searchfield(attr)disabled is true, make the value("searching....")

I could be wrong, but that is just a guess that it is a conditional statement.