XiLab XiLab - 2 months ago 7x
Javascript Question

jquery find parents of specific inputs and apply class

I have a selection div with checkboxes inside and I'm trying to style them with bootstrap buttons and jQuery. Each

input checkbox
takes the attribute
by php. So what I need is:

  • find all the
    which are 'checked', go to their
    labels and eventually
    addClass active

My html (in php foreach loop)

global $wpdb;
$ss = "select * from ".$wpdb->prefix."project_email_alerts where uid='$uid'";
$rr = $wpdb->get_results($ss);
$terms = get_terms( 'project_cat', 'parent=0&orderby=name&hide_empty=0' );
foreach($terms as $term):
$chk = (KleeiaDev_check_list_emails($term->term_id, $rr) == true ? "checked='checked'" : "");?>
<div data-toggle="buttons" class="btn-group bizmoduleselect">
<label id="b-<?php echo $term->term_id; ?>" class="btn btn-default">
<div class="bizcontent">
<input id="a-<?php echo $term->term_id ?>" type="checkbox" name="email_cats[]" <?php echo $chk ?> value="<?php echo $term->term_id ?>" />
<span class="glyphicon glyphicon-ok glyphicon-lg"></span>
<h5><?php echo $term->name ?></h5>
<?php endforeach; ?>

I've tried with jQuery at this way, searching on other similar questions without success:


I've tried with some use of .each but not really sure about that.


You can use


    color: red;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <label >
    <input type='checkbox' checked='true'> Check me
    <input type='checkbox'> Check me