Reputation: 577
I'm trying to add a conditional to my jQuery. My script is:
$("#mycontent a.half.first"+$id).removeClass("half").prependClass("paid");
$("#mycontent2 a.half.second"+$id).removeClass("half").prependClass("paid");
I have a link in a table:
<table class='grav-results-table' id='mycontent' width='75%'>
<tr class="grav-results-tr">
<td class="grav-results-td">
<a href="#inbound" class="half first3547" onclick="comtype(3547,'inbound', 704)">$184.20</a>
I'm trying to target the link shown above. I've tried the following with different variations:
if ( $( "#mycontent" ).hasClass( "half first" ) ) {
$("#mycontent a.half.first"+$id).removeClass("half").prependClass("paid");
$("#mycontent2 a.half.second"+$id).removeClass("half").prependClass("paid");
}
I have also tried:
if ( $( this ).hasClass( "half first" ) )
This code is in the success area of my AJAX script.
But I can't seem to find the correct way to target the class. What am I doing wrong?
Upvotes: 2
Views: 786
Reputation: 475
You need to change you if condition to look for the class in the children of the table... like this:
if ($("#mycontent").find("a.half").length > 0) {
console.log('children found');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class='grav-results-table' id='mycontent' width='75%'>
<tr class="grav-results-tr">
<td class="grav-results-td">
<a href="#inbound" class="half first3547"></a>
</td>
</tr>
</table>
Upvotes: 1
Reputation: 433
TO add to Martin you can try this.
if ( $( "table#mycontent" ).children().hasClass( "half first" ) ) {
alert("I have class in it.");
}
Upvotes: 0