Mahe
Mahe

Reputation: 2737

change style if the checkbox comes checked when page loaded

I am using a usercontrol in which a gridview with html checkboxes are present.I am using this control in another page. Some of these checkboxes comes checked when the user control in the page is loaded.

So in order to change the style of checkboxes when the usercontrol loads, this is the jquery code I have added in a js file,

<input type="checkbox" class="chk">

jQuery('input:checkbox.chk').click(evaluate).each(evaluate);

function evaluate() {
    debugger;
    var item = jQuery(this);
    alert('hello');
    if (item.is(":checked")) {
        jQuery(this).parent().toggleClass("active");
    } else {}
}

But this is not firing at all. what's wrong in the code. Can any one please help me out in solving this.

Any help is appreciated. Thanks.

Upvotes: 0

Views: 270

Answers (3)

fanfan1609
fanfan1609

Reputation: 179

May be you miss $(document).ready

$(document).ready(function(){
   // Put your code here
})

Upvotes: 2

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

Move this code to pageinit function of first page or document.ready event.

if (item.is(":checked")) {
    jQuery(this).parent().toggleClass("active");

} 
else {

}

Upvotes: 1

Adil
Adil

Reputation: 148120

There are three things to do

  • Add code in doucument.ready
  • You do not need each here.
  • use change instead of click

Binding event,

$(document).ready(function(){
    jQuery('input:checkbox.chk').change(evaluate);
});

Defining function 

 function evaluate() {
    debugger;
    var item = jQuery(this);
    alert('hello');
    if (item.is(":checked")) {
        jQuery(this).parent().toggleClass("active");
    } else {
    }
  }

Upvotes: 1

Related Questions