In this topic, we going to see how we can check the table contains text using jquery. As many users getting the issue of finding text in div or table.
To check if the table is contains text or not we can use contain selector, of Jquery.
:contains() Selector – jquery table text
But now important point is how to use this contain selector to find the text in table or div element. Before going to discuss regarding finding of value, first we will going in detail regarding what is this selector.
What is :contains() Selector ? – jquery table text
It is nothing but the selecting all elements that contain the specific text.
In the above text is nothing but the a string of text to look for and it is a case sensitive.
Example : Finds 2 color them red.
<head> <meta charset="utf-8"> <title>contains demo</title> <script src="https://code.jquery.com/jquery-3.5.0.js"></script> </head> <body> <table id="tableid"> <tr> <td>Some Value</td> <td>2</td> </tr> </table> <script> $('#tableid tr > td:contains(Some Value) + td:contains(2)').css("color", "red"); </script> </body> </html>
Some Value 2
Important point about the selector:contains() is that will return the element if the given string is completely or exactly matched anywhere in the text of the element, so be aware about this point.
In the above full html tag element contains a table which consist of single row table and it contains two column contain data.
So to get the value or string in the table i have used jquery :contains() Selector which will find exact match text or string in table and return the result.
The main and very important point is that matching text can be appear instantly or directly within the given selected element, The main point is that in any of that element’s descendants, or a combination thereof. As with this attribute selectors method in jquery, we can easly to say that text or string value inside the contain parentheses ie :contains() which can be written as a bare word or always surrounded by quotation marks which is important in every manner. Finally main thing is the text must have match case to be which is to be selected.