user3084703
user3084703

Reputation: 43

Checkbox onclick in function

I'm trying to execute some code while my checkbox is checked (onclick)

Here is the code of the checkbox:

<input type="checkbox" onclick="costTimeCalculator(this);" name="option_price" id="option_price" class="option_checkbox" value="Premium" >

And here is a part of the function:

function costTimeCalculator(totalwords)
{
    if(document.getElementById("option_price").checked)
    {
        var cost=totalwords*0.02
    }
    else
    {
        var cost=totalwords*0.015
    }

    cost=cost.toFixed(2);
    var  time=2;

    more code..............
    }

The function works, but not at the same moment that I'm checking the checkbox. What am I missing?

Upvotes: 2

Views: 479

Answers (1)

ilias
ilias

Reputation: 1345

Perhaps you don't call your function properly. Try this:

$('#option_price').on('click', function(){
    if(this.checked){
        var cost=totalwords*0.02
    }else{
        var cost=totalwords*0.015
    }
})  
    cost=cost.toFixed(2);
    var  time=2;

Upvotes: 1

Related Questions