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 trialThomas Marren
3,626 PointsHow does .text() add the text between the <p></p>?
Why does .text()
add the text between the <p></p> instead of replacing the <p> tags and thus making var $caption = $(captionText);
?
I'm just confused about what is exactly happening and how jQuery knows to place the text between two HTML tags.
5 Answers
Kenneth Black
23,140 PointsjQuery selectors target DOM elements (in this case the target is that paragraph element). The .text() method acts upon the contents of the DOM element, which is whatever is between the <p> tags. The distinction here is that, unlike putting HTML tags inside of strings, where they are just like any other characters, the jQuery statement $('< p >') is actually a DOM element, one that you can add attributes and properties to even before you append it to the page.
Trace Harris
Python Web Development Techdegree Graduate 22,065 Pointsfirst we created a Jquery object which in this case is a node and stored that in a variable think of it as a empty container the container being the p tags .("<p> empty</p>")
var $caption = $("<p></p>");
then we define the text we are going to use for our caption this is done by using another variable
var captionText = $(this).children("img").attr("alt);
so now this variable captionText equals the alt attribute of all the children of $(this) ($this )in our case is the clicked link of the image
so now comes the .text() method, .text() gets the text from a referenced element and returns that value as a string. .text() also is able to set strings as well. in this case we are doing both simultaneously.
$caption.text(captionText);
So we are using the .text() method to get the text of the variable captionText ( which again equals the alt attribute of all the children of $(this) ($this )in our case is the clicked link of the image ) returning that text as a string and setting that string as content for the Jquery object $caption so we are setting the string inside a container the paragraph tags.
the method .text() cannot replace our <p></p> tags because .text() does not get or set a dom element only a string. which is a datatype So it cannot set or become a container such as our <p></p> it can only be use to get or set a data type which we use to fill our empty container.
Tommy Gebru
30,164 PointsHey Thomas,
.text() is a method known as a content filter meaning it either retrieves or updates the content of elements (, attributes and text nodes).
Thomas Marren
3,626 PointsThanks for your help!
Leonardo de Deus
8,484 PointsI had the same doubt. But you guys are great helping out people. Thanks a lot! =]
Kenzo Fry
8,929 PointsI also had this question - thanks for the great answers everyone :)
Thomas Marren
3,626 PointsThomas Marren
3,626 PointsThanks, makes more sense now!