Find elements with text using jQuery
source link: https://www.codesd.com/item/find-elements-with-text-using-jquery.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Find elements with text using jQuery
I want to create an array of all the html elements within a div that contain text strings, such as
<p>some string</p>.
I don't want to get hold of the strings, I want the array items to be the elements (in the example, would be the p node). I do not know before hand what the strings will be, so I can't look for string values to match. I also don't want empty text nodes to end up in the array.
Thanks!
$("#my_div *").filter(function()
{
var $this = $(this);
return $this.children().length == 0 && $.trim($this.text()).length > 0;
})
This version will not return parent elements that contains the elements having texts, only the last level elements.
May not be the fastest but works quite well on StackOverflow homepage :)
Related Articles
Wrap the XML element with text using Java?
How to select the button with text using jQuery
Find elements in DOM using Jquery and delete
Alert HTML elements with .ajax using jQuery
Access normal text using jquery
Get span text using jquery
Hide n: text using jquery
Semi-transparent element with text that, when scrolling, goes over one navigation bar, but below the other
to add a class to some images based on alt text using jquery?
Exchange one list item with another using jQuery?
JQuery: how to write a selector to find elements with an end of class with a given substring?
jQuery find element with id in the line
Editing element text using jQuery
XML parsing with namespaces using jQuery $ (). find
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK