Solved

jquery text contain find

Posted on 2012-04-09
4
364 Views
Last Modified: 2012-04-09
Hello Experts,

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

TASK:

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


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

                        } // do something else

                 }
           

Open in new window

0
Comment
Question by:Refael
  • 2
  • 2
4 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37825370
Assuming that your table is something like this:-
<table id='tblTest'>
        <tr>
            <td>abc</td>
            <td>xyz</td>
            <td>are</td>
        </tr>
    </table>

Open in new window

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

Open in new window

0
 

Author Comment

by:Refael
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)
		return;	
			
		var text = secondTd.text();
		
		alert (text);
		
		if ($(this).text() == "2. FLOOR") {
			
		$('#ausblicke-etagen').show()
		}

Open in new window

0
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 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();
if(txt.indexOf("xy")>=0)
{
//Yes the text starts with xy
};
0
 

Author Closing Comment

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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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)

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now