1

I'm trying to find a specific row in a table, where the td value in the first column is the_month and the td value in the second column is the_year.

var the_month = 3,
    the_year = 15;
var tableRow = jQuery("td").filter(function() {
            return jQuery(this).text() == the_month;
        }).siblings().filter(function() {
            return jQuery(this).text() == the_year;
        }).parent('tr')

This code is almost what I want, but it's looking at every td. How can I limit this to only look for the_month in column 1 and the_year in column 2.

Edit: I understand the lack of clarity now. I have a huge table. I'm trying to find the row where the value of the first column is equal to the variable I created, called the_month, which is a number, and the value of the second column is the_year, which is also a variable I created. These variables are numbers. The code I originally posted keeps finding the value of the_month, let's say the_month = 3, in other columns of the wrong row. So I want to look for a row that has the number 3 in the first column and the number 15 in the second column.

8
  • How can I limit this to only look in the correct columns? is a bit unclear. I mean everything is unclear. What means "correct"? what columns? what rows? what? :) Commented May 5, 2015 at 16:26
  • 1
    Which columns are the "correct" columns? Can you post the table HTML? Commented May 5, 2015 at 16:27
  • "I want to find the row where the td value in the 1st column is the_month and the td value in the 2nd column is the_year." I'm not sure how much more specific I can get. The HTML is really long, there's 16 columns, and dozens of rows, which is why I didn't post it (also contains some sensitive data). Commented May 5, 2015 at 17:31
  • are you open to using different js libraries for this or do you want a modification in this code itself? Commented May 5, 2015 at 17:52
  • It seems like you should put your second query inside of your first. Basically, find the month in the first td, then if that month matches, find the year. Does this sound like what you're wanting to do? Commented May 5, 2015 at 17:59

2 Answers 2

2

Try this. This would have your first and second td's filtered out. http://jsfiddle.net/66za2f8n/2/

//you can modify the select to be #idofTable tr
    var tableRow = $("table tr").filter(function() {

                return $(this).find("td").eq(0).text() == "the_month" &&
                    $(this).find("td").eq(1).text() == "the_year";
            });


    console.log(tableRow);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but this doesn't return what I need. the .eq(1) only finds the first td, it doesn't look at the entire table. .eq() returns the index, which is 0 based, btw.
Sorry, still not working for me. I'll post the issue as soon as I've done some more testing
I didn't mean to imply by the lack of response that it didn't. I ended up marking @ntgCleaner's answer as correct because I used it. We bounced back and forth on some confusion caused by my imprecise descriptions on the problem, and when I got things working, I just marked his as the answer. And personal time and a long weekend to the non-response MO. I do appreciate the help.
1

If your code works as it stands, and I'm understanding you correctly, instead of making your query search for month and year, you should make it search for month then year

I haven't used filters, but I've made what you're looking for (I think) in THIS FIDDLE

Here's the accompanying code:

$('tr').each(function(i){
    var month = $(this).find('td').eq(0).text();
    if(month == 'the_month'){
        var year = $(this).find('td').eq(1).text();  
        if(year == 'the_year'){
            var tableRow = i;
            console.log(i);  
        }
    }
});

Now, since you didn't have any html posted, I can only assume what I've written is what you meant.

8 Comments

jsfiddle.net/32n4geee/2 This fiddle has the table code, and a few tweaks. Finds nothing.
@AnthonyRoberts Actually, it works perfectly. check the console - it found row 2 to be the row you're looking for.
Ok, it but it's logging the row number. What would the actual row be? I want it to return the row, not a number.
ok, add a second var to the each function called row, and then set tableRow = row, and this is correct. If you update, I'll mark it correct. Also, I'm going to delete the table code in the fiddle, so you might want to remove the link to it.
@AnthonyRoberts, I'm sorry, I don't understand how you mean "the actual row". Row 2 is the actual row. If you want the actual row minus the table headers, it would be row-2, bring it to row 0. Is this what you're asking for?
|

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.