Go Premium for a chance to win a PS4. Enter to Win


jQuery show/hide table rows

Posted on 2013-11-06
Medium Priority
Last Modified: 2013-11-06
Hello Experts,

I am using jQuery.....

I have a long table such as in the example below. What i am trying to do is hiding all the "tr" in the table but the ones i would like to keep such as in the array.

 0.1.5           Blue          86,70
 1.5.3           Red         113,60
 2.2.1           Yello          21,45
 1.5.7           Green      120,63

so for example i want to show only the rows ("tr") that its first td contains

0.1.5 and 2.2.1 and .......

Of course the first row (titles) should stay on.

Since this is a long list table its better to have the model numbers in an array.

Thank you for the help!
Question by:Refael
  • 3
  • 2
LVL 30

Expert Comment

by:Alexandre Simões
ID: 39628804
something like:

var hideThese = ['0.1.5', '2.2.1'];

$.each('#tableId tr', function(idx, item){
     var $row = $(item);
     var $cell = $($row.find('td')[0]);

     if(!$.inArray($cell.html(), hideThese))

Open in new window

Sorry I couldn't test it...
LVL 44

Accepted Solution

Rainer Jeschor earned 2000 total points
ID: 39628848
or something like this:

function HideTableRows() {
    var rowsToKeep = ["0.1.5","1.5.3","2.2.1","1.5.7"];

    $("#mytable > tbody > tr").each(function(){
        if ($.inArray($(this).find('td:first').text(), rowsToKeep) == -1) {

Open in new window


Author Comment

ID: 39628873
Hi RainerJ

Works perfect! I just amended it a bit.

AlexCode, not only that it did not work but also it prompt for few errors in the code :-(
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Author Closing Comment

ID: 39628874
Works,  Perfect! Thank you!
LVL 30

Expert Comment

by:Alexandre Simões
ID: 39629441
@Refael: sorry mate, I wrote it on the phone. It's proven not to be a good practice :)
LVL 30

Expert Comment

by:Alexandre Simões
ID: 39629451
Heck... I can^t leave a crappy answer like this :)
You can see it in action here: http://jsfiddle.net/3kNTY/
Here's my code anyway:
var hideThese = ['0.1.5', '2.2.1'];

function hideRows(values) {
    var searchColumnIndex = 0;
    $('#myTable tr').each(function (idx, item) {
        var $row = $(item);
        var $cell = $($row.find('td')[searchColumnIndex]);

        if (!$.inArray($cell.html(), values)) $row.hide();
        else $row.show();


Open in new window

Cheers mate!

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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)
Suggested Courses

971 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