Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select inner text with jQuery

<div class="boxen checkedzahlen" id="box41_0_1">
 41
 <input type="checkbox" value="1" name="cb41_0_1" id="cb41_0_1" checked="checked"/>
</div>

Something like this is given, how can I animate the text 41, if $(this) (the class boxen) is clicked?

this > * doesn't work. Neither does this:children.

like image 238
Sven Klouem Avatar asked Oct 29 '09 15:10

Sven Klouem


People also ask

How do I get inner text in jQuery?

Answer: Use the jQuery text() method You can simply use the jQuery text() method to get all the text content inside an element. The text() method also return the text content of child elements.

How do I get text inside a div using jQuery?

To get the value of div content in jQuery, use the text() method. The text( ) method gets the combined text contents of all matched elements. This method works for both on XML and XHTML documents.

How do I get text within an element?

Use the textContent property to get the text of an html element, e.g. const text = box. textContent . The textContent property returns the text content of the element and its descendants. If the element is empty, an empty string is returned.

How do I select a span containing a specific text value using jQuery?

To select a span containing a specific text value using jQuery, we can select all the spans and then use the filter method to find the one with the given text value. We call $(“span”) to get all the spans. Then we spread the spans into an array with the spread operator.


2 Answers

Because of the slightly confusing title, some people (like me) may be coming to this question looking for how to get the textual content of a DOM element. You can accomplish this by:

$("#myselector").text() // returns the content of the element as plain text
like image 158
Mike Chamberlain Avatar answered Sep 25 '22 13:09

Mike Chamberlain


$("#divID").html() would get the text inside.

like image 5
tuanvt Avatar answered Sep 27 '22 13:09

tuanvt