samedi 26 mars 2016

jquery checkbox hasclass and is checked

I'm handling a checkbox.

if( $(this).hasClass("select")) //To check class name
{
            if( $(this).is(':checked')) //to check whether it is checked
                sum =  parseInt($(this).parent().next().text()) + sum;
            else
                sum = sum - parseInt($(this).parent().next().text());

            alert('sum--' + sum);
}

now I have 2 if loops. How to merge them in single condition?

I've tried

1)

if( $(this).hasClass("select").is(':checked')) 
{
       //statements
}

It gave me an error ".hasClass("...").is('...')" is not a function.

2)

if( $(this).hasClass("select") && $(this).is(':checked')) 
{
    sum =  parseInt($(this).parent().next().text()) + sum;            
}
else if ( $(this).hasClass("select") && $(this).not(':checked'))
{
    sum = sum - parseInt($(this).parent().next().text());
}

It's working.

Question: How to merge these two in single statement in a simplified manner.

Can we simplify the condition? If yes how?




Aucun commentaire:

Enregistrer un commentaire