my question is how can i clone , lets say a calculater element keeping all the bindings to events like add , mul , sub , etc working in all the other duplicates. i tried so many ways and nothing works.
$("#newCalc").on( 'click' , function(){
$('#calcObj').clone(true , true).insertAfter('#calcObj');});
the last one i left for now. but when i am adding in the new calc it adds in the old one , same with all the other clones.
this is my code(its my first time programming js so forgive me for the bad coding):
$("#newCalc").on( 'click' , function(){
new calculator();});
/* Bind event with the corresponding functionality. */
$("#clear").on('click', calculate.clear);
$("#add").on('click', calculate.add);
$("#multiply").on('click', calculate.mul);
$("#subtract").on('click' , calculate.sub);
$("#divide").on('click' , calculate.div);
$("#settings").on('click', calculate.settings);
$(".num").on('click', function(){
$("#input").val(this.value);
});
$("#input").on('keyup', validateNumber);
});
//var calculate = new calculator();
/* Let's only digits, 0-9, to be written on input box. */
function validateNumber() {
if (isNaN($("#input").val())) {
alert("Only numbers!!! try again :)");
$("#input").val("");
}
}
function calculator(){
var empty = '';
var order = counter;
var el3_calc = "" +
'<table id="calcObj">' +
'<th colspan="3">' +
'<h1 id="calcHeadline"><b>Calculator</b></h1>' +
'</th>' +
'<tr>' +
'<td colspan="3">'+
'<input type="text" id = "input" class="calcIO" value="">'+
'<input type="button" id ="add" value="+">'+
'<input type="button" id ="multiply" value="x">' +
'<input type="button" id ="subtract" value="-">' +
'<input type="button" id ="divide" value="/">' +
'<input type="text" id="result" readOnly="true" value="" class="calcIO"><br></td>' +
'</tr>' +
'<tr>'+
'<td><input type="button" class ="num" value="1"></td>'+
'<td><input type="button" class ="num" value="2"></td>'+
'<td><input type="button" class ="num" value="3"><br></td>'+
'</tr>'+
'<tr>'+
'<td><input type="button" class ="num" value="4"></td>'+
'<td><input type="button" class ="num" value="5"></td>'+
'<td><input type="button" class ="num" value="6"><br></td>'+
'</tr>'+
'<tr>'+
'<td><input type="button" class ="num" value="7"></td>' +
'<td><input type="button" class ="num" value="8"></td>' +
'<td><input type="button" class ="num" value="9"><br></td>' +
'</tr>' +
'<tr>'+
'<td><input type="button" id="newCalc" value = "AddCalc"></td>'+
'<td><input type="button" class ="num" value="0"></td>'+
'<td><input type="button" id="clear" value="Clear"></td>'+
'</tr>'+
'</table>';
$("#third").append(el3_calc);
counter += 1;
this.add = function(){
if($("#input").val() != empty && $("#result").val() == empty){
$("#result").val($("#input").val());
}
else{
$("#result").val(parseFloat($("#input").val()) + parseFloat($("#result").val()));
}
$("#input").val(empty);
}
this.mul = function() {
if($("#input").val() != empty && $("#result").val() == empty){
$("#result").val($("#input").val());
}
else{
$("#result").val(parseFloat($("#input").val()) * parseFloat($("#result").val()));
}
$("#input").val(empty);
}
this.div = function(){
if($("#input").val() != empty && $("#result").val() == empty){
$("#result").val($("#input").val());
}
else{
$("#result").val(parseFloat($("#result").val()) / parseFloat($("#input").val()));
}
$("#input").val(empty);
}
this.sub = function(){
if($("#input").val() != empty && $("#result").val() == empty){
$("#result").val($("#input").val());
}
else{
$("#result").val(parseFloat($("#result").val()) - parseFloat($("#input").val()));
}
$("#input").val(empty);
}
this.clear = function() {
$("#result").val(empty);
$("#input").val(empty);
}
}