Need help with a checkbox

Discussion in 'JavaScript' started by qwikad.com, Oct 25, 2016.

  1. #1
    This works fine, except the var total = 0; doesn't show unless you click the checkbox twice. How can I show that 0 right away when the checkbox is first loaded?

    https://jsfiddle.net/zf1psgw6/1/

    
    <input type="checkbox" value="1" id="add_one" >
    <div id="total"> </div>
    
    Code (markup):
    
    $("#add_one").click(function () {
    var chk = $("#add_one").val();
    var total = 0;
    if ($("#add_one").prop("checked") == true) {
    var gtotal = parseInt(total) + parseInt(chk);
    } else {
    var gtotal = parseInt(total);
    }
    $('#total').text(gtotal);
    });
    
    Code (markup):
     
    qwikad.com, Oct 25, 2016 IP
  2. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,291
    Likes Received:
    1,698
    Best Answers:
    31
    Trophy Points:
    475
    #2
    qwikad.com, Oct 26, 2016 IP
    sarahk likes this.