jquery text contain find

Posted on 2012-04-09
Medium Priority
Last Modified: 2012-04-09
Hello Experts,

Here is what i am trying to do.... hopping someone will correct me :-)


Check if the text in the second TD (table column) starts with "xyz".

var secondTd = $('td.eq(2)', $(this));
        if (secondTd.length == 0)
	   var text = secondTd.text();	
		if (text == "xyz") {
		//do something....................		
                 else {

                        } // do something else


Open in new window

Question by:Refael
  • 2
  • 2
LVL 20

Expert Comment

ID: 37825370
Assuming that your table is something like this:-
<table id='tblTest'>

Open in new window

Here is the jquery for comparison:-
$('#tblTest tr td:nth-child(2)').text() == "xyz"

Open in new window


Author Comment

ID: 37825442
Hi BuggyCoder,

that's wrong because you check if the entire TD text string is equal ... and i am after the beginning of the string or if its easier then i can use "contain".

Here is my code again..... if you can just tell me where the problem is:

var secondTd = $('.tdlight:eq(1)', $(this));
		if (secondTd.length == 0)
		var text = secondTd.text();
		alert (text);
		if ($(this).text() == "2. FLOOR") {

Open in new window

LVL 20

Accepted Solution

BuggyCoder earned 2000 total points
ID: 37825456
here is the code that will check if the text of td starts with xy:-

var txt = $('#tblTest tr td:nth-child(2)').text();
//Yes the text starts with xy

Author Closing Comment

ID: 37825508
Great, that's what i was looking for :-)

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
AngularJS web development a very simple procedure. So, to put it, in short, AngularJS’ stand out features are – Two-way data binding, MVC structure, directives, templates, dependency injections and testing.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question