2

I have two tables at the moment. What Im looking to achieve is to select a row in one table, obtain the "filename" field from that and then check if that filename exists in the other table. If the file exists in both tables I want to change the colour of my progress tracker. Right now I have the selecting of the row working, but I can't seem to check it against the other table. Any help would be greatly appreciated.

HTML:

<table id="table">
  <tr>
    <td>--</td>
    <td>Filename</td>
  </tr>
  <tr>
    <td>1</td>
    <td>Example1</td>
  </tr>
  <tr>
    <td>2</td>
    <td>Example2</td>
  </tr>
</table>

<table id="table2">
  <tr>
    <td>--</td>
    <td>Filename</td>
  </tr>
  <tr>
    <td>1</td>
    <td>Example1</td>
  </tr>
</table>

<div id="words">
</div>

JavaScript:

$("#table").find("tr").click(function(){
    $(this).addClass('selected').siblings().removeClass('selected');
    var value=$(this).find('td:nth-child(2)').html();
    //alert(value);
    document.getElementById("words").innerHTML = value;
});

Thanks again for the help!

5
  • 3
    Posting the actual HTML would be helpful to provide a better answer, or post a link to a JSFiddle of what you've got so far. Commented Feb 23, 2016 at 15:18
  • 1
    if ($('#table2 td:contains(' + filename + ')').length) // do stuff Commented Feb 23, 2016 at 15:20
  • Sorry I just added the HTML, the fields are being populated from files using php so I can't really show a lot of whats happening Commented Feb 23, 2016 at 15:36
  • I'll upvote your question, since it's difficult to do certain things when you're new, but this site is set up to protect from spammers/scammers. You can also think about changing your username. A few upvotes should be all you need to comment and do other things. Commented Feb 23, 2016 at 17:48
  • Thanks a lot @vol7ron! Commented Feb 24, 2016 at 9:49

2 Answers 2

2
$("#table").on('click','tr',function(){                   // <-- #1
   var $this    = $(this),                                // <-- #2
       filename = $this.find('td:nth-child(2)').text(),   // <-- #3
       $words   = $('#words');

   $this.addClass('selected').siblings().removeClass('selected');
   $words.html(filename).css('color','black');

   if ( valueInTable('table2', 1, filename ) ){           // <-- #4
     $words.css('color', 'blue');
   }

});

function valueInTable(tableID, columnNum, searchString){
  var found = false;
  $( '#' + tableID + ' tr td:nth-child(' + columnNum + ')' ).each(function(){
    if ($(this).text() == searchString ){
      found = true;
      return false;
    }
  });

  return found;
}
  1. This is important, this binds the event to the table. When a click occurs somewhere inside the table it checks the event registry, in this case, it checks to see if a TR was clicked. This is both a performance gain, since you're not creating an event for each row of the table, but also if you create new rows dynamically, you don't have to create a new event when you do. You create this event once and it's in place for all new/old rows of the table

  2. Cache $(this) into a variable. You use it more than once and chances are you'll use it even more. You should not create a new jQuery object every time you want to refer to $(this), so stick it in a variable and reuse that

  3. While .html() may work for you, if you have other embedded HTML, you might get values you were not intending (e.g., <span>filename</span>), for that reason, you only need .text(), which will just give you the text value and strip off all the nested HTML (leaving you with only filename)

  4. Using a function comes with a penalty, but it's good to put long-logic elsewhere, in case you're doing anything more involved. For instance, your table could expand in width (number of columns) and you might also want to search them for a value, or you might have more tables you want to look in; this same function can be used for both of those cases.

as noted, the :contains() selector was built for what you're after However, there is one caveat. The problem with contains is that it lacks customization. If you want to modify your comparison to be a RegEx, or if you want to perform other manipulation using trim or truncate, you can't do that with contains. You could easily modify the code below to do: $.trim( $(this).text() ) == $.trim( searchString )

Sign up to request clarification or add additional context in comments.

1 Comment

@user please refer to this edit as it points out changes that are worth consideration, which were present, but not outlined/described beforehand. There are many other things I could have done, but this I kept the number of changes low, since it's a quick learning tool going forward. That said, if you are new to the language, you already have demonstrated better understanding than some other seasoned developers
1

As @Pete commented, you can use if ($('#table2 td:contains(' + value + ')').length) as follows

$("#table").find("tr").click(function(){
    $(this).addClass('selected').siblings().removeClass('selected');
    var value=$(this).find('td:nth-child(2)').html();
    //alert(value);
    if ($('#table2 td:contains(' + value + ')').length) {
        document.getElementById("words").innerHTML = value;
    } else {
        document.getElementById("words").innerHTML = "false";
    }
});

See the JSFiddle for working example: https://jsfiddle.net/v14L4bqr/

6 Comments

contains is probably the better choice, for some reason I thought it was deprecated
you oculd also use .filter()
That works perfectly! Thanks for your help, I can't vote it up until I get a reputation of 15 but I'll be sure to do it as soon as I do!
@user you can select it as an answer; you and Kishor will receive points once you do
Sorry, didn't realise I could do that! I've done it there now, Thanks again for the help!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.