Is there a succint way of getting just the text in a div, that also has child elements in it, using jQuery?
For example - how would I extract "Some text" from the html below:
<div id="mydiv">
<img src="...
Some text
</div>
Is there a succint way of getting just the text in a div, that also has child elements in it, using jQuery?
For example - how would I extract "Some text" from the html below:
<div id="mydiv">
<img src="...
Some text
</div>
var text = $("#mydiv")
.clone() //clone the element
.children() //select all the children
.remove() //remove all the children
.end() //go back to selected element
.text(); //get the text of element
I tried your example with
$('#mydiv').text();
and it worked fine for me!
Using .text() function to any element will extract the text within that tag
$("#elementname").text(); will work
here is the sampe Demo
whatever
it doesn't work – Graham Jan 10 '13 at 11:42