Search code examples
jqueryfindparentchecked

Add class if element is checked, but on load not click


So I have this html code:

<div class="panel-radioimage">
<div class="radio-image-wrapper">
<label for="1"><img src="http://127.0.0.1/wordpress/wp-content/themes/bluestudio5/images/patterns/1.png" alt="image" class="admin-patterns">
<div class="check-list"></div></label>
<input class="farm_img_pattern" type="radio" name="farm_img_pattern" value="1.png" checked>
</div>

I have few of these elements on page, and I want to add class "checked-list" to class "check-list" when input is checked on page load. SO I tried this that worked before with click event:

 if(jQuery('.farm_img_pattern').is(':checked')){
        jQuery(this).parent().find(".check-list").addClass("checked-list");
    }

but its not working. Problem is with "this" Anyone know solution for getting checked element on page load ?


Solution

  • I think you want something more like this:

    jQuery('.farm_img_pattern').filter(':checked').each(function(index) {
        jQuery(this).parent().find(".check-list").addClass("checked-list");
    });