• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 427
  • Last Modified:

jquery text contain find

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
Refael
Asked:
Refael
  • 2
  • 2
1 Solution
 
BuggyCoderCommented:
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
 
RefaelAuthor Commented:
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
 
BuggyCoderCommented:
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
 
RefaelAuthor Commented:
Great, that's what i was looking for :-)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now