I have TRs that are added dynamically after the page is loaded. When they are clicked, I want to add the class "Active" to them. Here is what I have so far:
<tr onClick="makeActive(this);"><td>....</td></tr>
<script>
function makeActive(element){$('.Active').removeClass('Active');element.addClass('Active');}
</script>
The function runs, and removes the "Active" class from any rows that currently have it applied...but it fails to add the class to the clicked row.
Thanks for any help!
this
references a DOM element, you have to wrap it inside a jQuery object first to apply a jQuery method to it.
$(element).addClass('Active');
You were close:
<tr onClick="makeActive(this);"><td>....</td></tr>
<script>
function makeActive(element){
$('.Active').removeClass('Active');
$(element).addClass('Active');
}
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With