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 trialWelby Obeng
20,340 PointsI saw andrew used .val() in line 15 instead of .attr("value", " "); why? what is the difference?
I saw andrew used .val() in line 15 instead of .attr("value", " "); why? what is the difference?
2 Answers
Baruch Velez
275 PointsLooking at the jQuery API (http://api.jquery.com/val/) you can see that using .val is better than .attr('value') simply because the .val() function accepts parameters and you can add other functions to that specific value you're selecting.
Also, "The gist is that .attr(...) is only getting the objects value at the start (when the html is created). val() is getting the object's property value which can change many times."
source: http://stackoverflow.com/questions/8312820/jquery-val-vs-attrvalue
shezazr
8,275 PointsNothing.. but val() i guess is just a wrapper (if not another way) of getting the value. I guess because getting a value of something is so common, its nice to have a shortcut (smaller code) than big one..