There are several elements that are selected by $(".foo")
. $(".foo").text()
returns the text of each element concatenated together. I just want the text of one element. What is the best way to do this?
$(".foo")[0].text()
fails.
jQuery css() Method The css() method sets or returns one or more style properties for the selected elements.
jQuery wrap() method is used to wrap specified HTML elements around each selected element. The wrap () function can accept any string or object that could be passed through the $() factory function. Syntax: $(selector).
To select elements with a specific class, write a period (.) character, followed by the name of the class. You can also specify that only specific HTML elements should be affected by a class.
jQuery toggle() Method The toggle() method toggles between hide() and show() for the selected elements. This method checks the selected elements for visibility. show() is run if an element is hidden.
You want to use .eq(0)
, like this:
$(".foo").eq(0).text()
When you do $(".foo")[0]
or $(".foo").get(0)
you're getting the DOM Element, not the jQuery object, .eq()
will get the jQuery object, which has the .text()
method.
Normally using the #
selector syntax selects one element by id
attribute value. Do you have more than one element with the same id
attribute value? If so, then you need to correct your HTML. id
attribute values should be unique within a document.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With