Ritz de Guzman Ritz de Guzman - 5 months ago 8
jQuery Question

Formatting appended text using JavaScript

HTML code

<button id="btn1">show item no. 1</button>
<button id="btn2">show item no.2</button>
<div id="resulttext"></div>


JavaScript code

price1=20;
price2=50;
qty1=0;
qty2=0;
amount=0;

$(document).ready(function(){
$('#btn1').click(function(){
qty1=qty1 + 1;
amount=price1*qty1;
$("resultqty").change(qty1);
$("resultprice").change(amount);
$("#resulttext").append("<div><button type='button' id='remove1' class='close pull-right' aria-hidden='true'>&times;</button>REGULAR BURGER" + "<p> Quantity:"+qty1+"</p><p> Unit Total:"+amount+"</div>");
})
})
$(document).on('click', 'button#remove1', function(){
$(this).parent().remove();
});

$(document).ready(function(){
$('#btn2').click(function(){
qty2=qty2 + 1;
amount=price2*qty2;
$("resultqty").change(qty2);
$("resultprice").change(amount);
$("#resulttext").append("<div><button type='button' id='remove2' class='close pull-right' aria-hidden='true'>&times;</button>SPECIAL BURGER"+ "<p> Quantity:"+qty2+"</p><p> Unit Total:"+amount+"</div>");
})
})
$(document).on('click', 'button#remove2', function(){
$(this).parent().remove();
});


The code above is actually working, but my main problem is that when I click a specific button, the text that must be appended should only appear once but the quantity and amount should still change, real-time, as you continuously click the same button.

And another thing is, I want to get the sum of the amount of both buttons and display it on the page.

Answer

EDIT:

Here the HTML:

<button id="btn1">show item no. 1</button>
<button id="btn2">show item no.2</button>
<div id="resulttext"></div>

And here the js:

price1=20;
price2=50;
qty1=0;
qty2=0;
amount=0;

$(document).ready(function(){
    $('#btn1').click(function(){

        qty1=qty1 + 1;
        amount=price1*qty1;
        $("resultqty").change(qty1);
        $("resultprice").change(amount);
        if($("#part1").length>0){
            $("#part1").html("<div><button type='button' id='remove1' class='close pull-right' aria-hidden='true'>&times;</button>REGULAR BURGER" + "<p> Quantity:"+qty1+"</p><p> Unit Total:"+amount+"</div>");
        }else{
            $("#resulttext").append("<div id='part1'><div><button type='button' id='remove1' class='close pull-right' aria-hidden='true'>&times;</button>REGULAR BURGER" + "<p> Quantity:"+qty1+"</p><p> Unit Total:"+amount+"</div></div>");
        }
    })
})
$(document).on('click', 'button#remove1', function(){        
    $("#part1").html("");
});


$(document).ready(function(){
    $('#btn2').click(function(){
        qty2=qty2 + 1;
        amount=price2*qty2;
        $("resultqty").change(qty2);
        $("resultprice").change(amount);
        if($("#part2").length>0){
            $("#part2").html("<div><button type='button' id='remove2' class='close pull-right' aria-hidden='true'>&times;</button>SPECIAL BURGER"+ "<p> Quantity:"+qty2+"</p><p> Unit Total:"+amount+"</div>");
        }else{
            $("#resulttext").append("<div id='part2'><div><button type='button' id='remove2' class='close pull-right' aria-hidden='true'>&times;</button>SPECIAL BURGER"+ "<p> Quantity:"+qty2+"</p><p> Unit Total:"+amount+"</div></div>");
        }
    })
});
$(document).on('click', 'button#remove2', function(){        
    $("#part2").html("");
});

fiddle here: http://jsfiddle.net/YLEXU/3/

Comments