Jon Jones Jon Jones - 9 days ago 6
jQuery Question

Turn JQuery into click event

I have the following code that works fine

$(window).load(function(){
$(document).ready(function(){
$("input[name ='_sft_product_cat[]']").parent().next(".children").css("background", "yellow");
})
});


What I am trying to do is turn this into a click event but I cant get it work, I have tried the following

$(window).load(function(){
$(document).ready(function(){
$("input[name = '_sft_product_cat[]']").click(function(){
$(this).parent().next(".children").css("background", "blue");
return false;
});


What am I doing wrong?

Thanks

Answer

What you miss is closing brackets for the document.ready and window.load functions. Also you need not use window.load and document.ready both , only document.ready is sufficient

$(document).ready(function(){
$("input[name = '_sft_product_cat[]']").click(function(){
 $(this).parent().next(".children").css("background", "blue");
return false;
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <input type="checkbox" name="_sft_product_cat[]"/>
</div>
<div class="children">Hello</div>