7

Find elements with text using jQuery

 3 years ago
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.
neoserver,ios ssh client

Find elements with text using jQuery

advertisements

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?

I want to transform XML like this: <account>Wally World</account> <city>Anywhere</city> <state>CA</state> Into this by wrapping each element with equals and single quote i.e. ='' <account>='Wally World'</accoun

How to select the button with text using jQuery

Is it possible to select buttons using the text property in jQuery? I was trying like: $.each(data, function(index, element){ if(element.table_aval == 0) { var elmt = element.table_no; $('#table button[text='+elmt+']').css('color','red'); } }); Is th

Find elements in DOM using Jquery and delete

Possible Duplicate: Clear 5 empty TDs with Jquery I want to search the DOM for any instance (might be multiple) where the DOM contains specifically <td></td><td></td><td></td><td></td><td></td> a

Alert HTML elements with .ajax using jQuery

Is there a way of alerting all of the HTML from this .ajax get? function testq() { var acronyms = ["fubar", "snafu", "gnu"]; $.each(acronyms, function(index, element) { $.ajax({ url: "https://encrypted.google.com/search?

Access normal text using jquery

I have the following HTML code: <div class="top_l"> <a class="shou" onclick="" href="">11</a> | <img border="0" class="" src="/">XXXXXX<a href="">

Get span text using jquery

Giving this html, i want to grab "August" from it when i click on it: <span class="ui-datepicker-month">August</span> i tried $(".ui-datepicker-month").live("click", function () { var monthname = $(this)

Hide n: text using jquery

I have this list <li id="field_7_div" class="field_block"> <div class="col_label"> <label class="form_field">How satisfied are you with <span class="aggregator_name">your aggregator

Semi-transparent element with text that, when scrolling, goes over one navigation bar, but below the other

How can I make a semi-transparent element with text in it which goes on top of one navigation bar, but goes under the other? I am trying to make an effect like in http://hongkong.grand.hyatt.com/en/hotel/home.html where the red Hyatt logo and "hong k

to add a class to some images based on alt text using jquery?

how can i add a class to some images based on the alt text using jquery? here is an example of the image: <img border="0" src="images/Product/icon/26086_1_.jpg" alt="Show Picture 1" onclick="setcolorpicidx_26086(1);&q

Exchange one list item with another using jQuery?

Is there any way to swap a list item with another using jQuery? assume this list: <ul> <li id="1"></li> <li id="2"></li> <li id="3"></li> <li id="4"></li> <li

JQuery: how to write a selector to find elements with an end of class with a given substring?

I use JQuery for my web application. I don't know how to write a selector for this situation. <a class="abc def xyz-delete-confirm efg">text</a> <a class="abc def delete-confirm">text</a> I need to find all link

jQuery find element with id in the line

I have a simple table, I'm trying to send text value from cells .name and .date into form using jQuery. This script works pretty well, but I don't know how find only text with class for example .name in only this row, where the user cliced on Book-bu

Editing element text using jQuery

I am new to jQuery and am developing some practice code for FF using Netbeans 7.3 on Ubuntu 11.4. I start out with the h1 element having the text "Hello world" using the following html call. <h1>Hello World</h1> I then try to change

XML parsing with namespaces using jQuery $ (). find

I'm trying to get the contents of a XML document element, but the element has a colon in it's name. This line works for every element but the ones with a colon in the name: $(this).find("geo:lat").text(); I assume that the colon needs escaping.

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK