1

I want to make some jQuery that shows some table rows and hides others based on the content of the first table cell in each row.

When I click a list item I want jQuery to check if the first letter of the item matches the first letter in any table cell in my markup, if so the parent table row should be shown and other rows should be hidden.

This is my markup:

<ul> <li>A</li> <li>B</li> <li>G</li> </ul> <table> <tr> <td>Alpha1</td> <td>Some content</td> </tr> <tr> <td>Alpha2</td> <td>Some content</td> </tr> <tr> <td>Alpha3</td> <td>Some content</td> </tr> <tr> <td>Beta1</td> <td>Some content</td> </tr> <tr> <td>Beta2</td> <td>Some content</td> </tr> <tr> <td>Beta3</td> <td>Some content</td> </tr> <tr> <td>Gamma1</td> <td>Some content</td> </tr> <tr> <td>Gamma2</td> <td>Some content</td> </tr> <tr> <td>Gamma3</td> <td>Some content</td> </tr> </table> 

So if I press "A" this is what is rendered in the browser:

<ul> <li>A</li> <li>B</li> <li>G</li> </ul> <table> <tr> <td>Alpha1</td> <td>Some content</td> </tr> <tr> <td>Alpha2</td> <td>Some content</td> </tr> <tr> <td>Alpha3</td> <td>Some content</td> </tr> </table> 

I'm really new to jQuery so any hint on how to go about a problem like this would be appreciated :)

2 Answers 2

4

Sotris is almost correct in his answer. I believe that what you want is:

$('li').click(function() { var letter = $(this).text(); $("td").hide(); $("tr").each(function(){ if ($("td:first:contains('" + letter + "')", this).length != 0) { $('td', this).show(); } }) }); 

If you are really interested only in comparing the first letter of the first "TD" in a row, and not any letter (":contains") then change the line that says:

if ($("td:first:contains('" + letter + "')", this).length != 0) { 

by:

if ($("td:first", this).text().substr(0,1) == letter) { 

Alternatively you could use a regular expression, e.g. replace:

var letter = $(this).text(); 

By:

var re = new RegExp('^' + $(this).text()); 

and the line that says:

if ($("td:first:contains('" + letter + "')", this).length != 0) { 

by:

if ($("td:first", this).text().match(re)) { 
Sign up to request clarification or add additional context in comments.

6 Comments

David, You're correct. I spotted this and modified the answer accordingly.
@Neil, I saw that; hence my deleting the comment =)
@David, I saw you'd deleted your comment so I tried to delete mine but couldn't.
@Neil, no problem. Assuming there's no rep required all you have to do is click on the 'x' near your name, that appears while hovering over your comment. =)
Found a slight problem with this - the solution works with the markup I've used as an example, however as I write I want to only check on the first letter. If there was e.g. a td that had "BETA" in it, it would false positive, due to the capital "A". Hope this makes sense :S
|
2

edit: I hadn't checked that it doesn't show and the second td so I changed a little my code:

 $('li').click(function() { var letter = $(this).text(); $("table td").hide(); $("table td:contains("+letter+"), table td:contains("+letter+") + td").show(); }); 

Demo: http://jsfiddle.net/rdBx9/1

or

$('li').click(function() { var letter = $(this).text(); $("table td").hide(); $("table td:contains("+letter+")").show().next().show(); }); 

Demo: http://jsfiddle.net/rdBx9/2

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.