0

Using Javascript, I'm trying to change the class of the first TD of a table row. Using a button which appears in the same row. what do I add before the "addClass" to make it only target only the row its in, and the first table data of that row?

$("table").on("click", ".checkbox", function() {
 .addClass("line");
});

2 Answers 2

1

you can use $(this) and .closest() to get the parent row .. and .find() to get the td and use :first to select first one

$("table").on("click", ".checkbox", function() {
   $(this).closest('tr').find('> td:first').addClass("line");
});

or you can use .first()

$("table").on("click", ".checkbox", function() {
    $(this).closest('tr').find('> td').first().addClass("line");
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your help sir. I was but 3 points away from being able to give your answer an upvote, yet sadly I'm now 5 points away. All I can offer is my gratitude.
wait, it's a miracle, I'm now up to 17! Huzzah!
@EliezerWohl able right now :) .. Very good luck to you :) .. Don't tell anyone that I upvoted you :) :) lol
0

Eliezer -

You are going to want to set a class for the button you are applying the click handler to. That way you can call the selector for all of those buttons. Then you will use the parent calls to get the TD that the button belongs to. It should look something like this:

$('.button-class').on('click', function() {
    $(this).parent.addClass('new-class');
});

Comments

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.