Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if children contain specific text with jQuery

I have to filter a html-table. To do so I created an each callback for all tr elements and test if one of the tr-children contain some specific pattern.

$("#filter").keypress(function() {
    var filter = $(this).val();

    $("#table1 tr[data-config]").each(function(){

        var val = $(this).find(":contains('" + filter + "')");

        if(val.length > 0){
            $(this).css("display","table-row");
        }else{
            $(this).css("display","none");
        }
    });
});

It works, but is there an function to test if a element contains some text?

At the moment I retrieve a list of all elements containing the pattern and count if it's bigger than zero. Is there a jQuery function, which tests if this pattern occurs and returns a boolean? The table can contain many rows and therefore I want as little overhead as possible.

like image 607
NaN Avatar asked Aug 06 '12 07:08

NaN


People also ask

How do I check if a div contains a text?

To check if a div element contains specific text:Use the textContent property on the element to get the text content of the element and its descendants. Use the includes() method to check if the specific text is contained in the div . If it is, the includes() method returns true , otherwise false is returned.

How do you get children of children in jQuery?

jQuery children() MethodThe children() method returns all direct children of the selected element. The DOM tree: This method only traverse a single level down the DOM tree. To traverse down multiple levels (to return grandchildren or other descendants), use the find() method.

How do I select a specific child in Javascript?

Select the parent element whose child element is going to be selected. Use . querySelector() method on parent. Use the className of the child to select that particular child.

How do you get the children of the $( this selector?

Answer: Use the jQuery find() Method You can use the find() method to get the children of the $(this) selector using jQuery. The jQuery code in the following example will simply select the child <img> element and apply some CSS style on it on click of the parent <div> element.


2 Answers

///// replace these lines...
// var val = $(this).find(":contains('" + filter + "')");
// if(val.length > 0){

///// with this line
if($(this).text().match(filter)){

This works by converting the whole row content into a text string, then using simple string comparison function to check if filter is contained inside the text (and therefore the row)

like image 88
Billy Moon Avatar answered Sep 29 '22 01:09

Billy Moon


The answers are good, but there's an easier way using :contains

In general, do this:

$(".class:contains('texttofind')").css("display", "none");

With the specific example above, do this:

$("#table1 tr[data-config]:contains('" + filter + "')").css("display","table-row");

You can find out more here: https://api.jquery.com/contains-selector/

This avoids having to loop with .each or .find.

like image 26
Dave Hilditch Avatar answered Sep 29 '22 00:09

Dave Hilditch