Solved

jquery text contain find

Posted on 2012-04-09
4
373 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
This article discusses how to create an extensible mechanism for linked drop downs.
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

831 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