1

So I have this button, you click it and it changes the background color from blue to red. If I wanted to have that button change the row background color instead, how would I do that? This is in a loop too so all the classes are the same, I use the below code so it only changes whatever I clicked instead of everything with that class.

script

<script type="text/javascript">
$('.a-row').on('click', '.H', function () {
    $( this ).css( "background-color", "red" );
});
</script>

html/php

<tr class="a-row">
 <td><p class="pname"><?=$row["Name"]?></p><p class="clickbutt H">H</p></td>
 <td>stuff</td>
 <td>stuff2</td>
 <td>stuff3</td>
</tr>

3 Answers 3

1

You can use .parents() method

<script type="text/javascript">
$('.a-row').on('click', '.H', function () {
    $( this ).parents("tr").css( "background-color", "red" );
});
</script>

Example

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

Comments

1

You can use .closest('tr'):

$('.a-row').on('click', '.H', function () {
    $(this).closest('tr').css( "background-color", "red" );
});

jquery .closest()

Comments

1

You can try the .closest() method of jquery like so.

<script type="text/javascript">
 $('.H').on('click', function () {
  $( this ).closest.('tr').css( "background-color", "red" );
 });
</script>

You can check out the documemtation here. https://api.jquery.com/closest/

Hope this wasa helpful.

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.