I have the following code:
<td class="CalendarDay CalendarDay--valid" style="width: 37px; height: 36px;">
<button type="button" class="CalendarDay__button">16</button>
</td>
Check button value if it is 16 then remove the .CalendarDay--valid
from the parent div.
How do I do this?
You can use closest()
method to do that:
var elem = $('.CalendarDay__button'); if (elem.text().trim() === '16') { elem.closest('.CalendarDay--valid').removeClass('CalendarDay--valid'); }
.CalendarDay--valid button{ color: red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <tr> <td class="CalendarDay CalendarDay--valid" style="width: 37px; height: 36px;"> <button type="button" class="CalendarDay__button">16</button> </td> </tr> </table>
Use parent
with this
for particular td
. I give two td
16 -- 17 on example.17 is shown but 16 remove.
$('.CalendarDay').find('.CalendarDay__button').each(function() { if ($(this).text() == '16') $(this).parent('td').remove(); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <tr> <td class="CalendarDay CalendarDay--valid" style="width: 37px; height: 36px;"> <button type="button" class="CalendarDay__button">16</button> </td> <td class="CalendarDay CalendarDay--valid" style="width: 37px; height: 36px;"> <button type="button" class="CalendarDay__button">17</button> </td> </tr> </table>
Try this if you have multiple buttons to check:
$.each($('.CalendarDay__button'),function(){ var btn = $(this); var parent = btn.closest('.CalendarDay'); // Before console.log(parent.attr('class')) if(btn.text() == "16"){ parent.removeClass('CalendarDay--valid'); // After console.log(parent.attr('class')) } })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <tr> <td class="CalendarDay CalendarDay--valid" style="width: 37px; height: 36px;"> <button type="button" class="CalendarDay__button">16</button> </td> </tr> </table>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.