jQuery: Using .is(“:checked”) method will return boolean value So now below written jQuery code, will display checkbox status .i.e whether the checkbox is checked or unchecked on button click.
var chkItem=$("#chkFollow");
$("#btnCheckStatus").on('click',function(){
if($(chkItem).is(":checked"))
{
console.log("Yes you are following ");
}
else{console.log("No you are not following");}
});
In above code 1st we stored the checkbox control in a variable, which we use when button click function trigger, basically assigning variable to control (selector)is good practise for performance wise, i.e. caching the control. Next we use [highlight].is(“:checked”)[/highlight] this return Boolean value, as a result, we got to know our checkbox current status, checked or unchecked.
var chkItem=$("#chkFollow");
$("#btnCheckStatus").on('click',function(){
if($(chkItem).is(":checked"))
{
console.log("Yes you are following ");
}
else{console.log("No you are not following");}
});
In above code 1st we stored the checkbox control in a variable, which we use when button click function trigger, basically assigning variable to control (selector)is good practise for performance wise, i.e. caching the control. Next we use [highlight].is(“:checked”)[/highlight] this return Boolean value, as a result, we got to know our checkbox current status, checked or unchecked.
0 comments:
Post a Comment