Raul Alberto Calzado Cuevas Raul Alberto Calzado Cuevas - 3 months ago 14
Javascript Question

How do I reset this variable?

I would like to know how to reset the variable

a
in my program.

What's the program about? It catches some IDs from a scanner and transforms them into a receipt.

How the code works? This part of the code that I'm showing you catches those IDs and fills them into a table, and when I press a button, in this case "btnRecibo", it cleans all the table data and the total number of IDs.

But here's the problem. When I clean all the data, the variable
a
keeps data saved in the background (the total) and it can't be reset along with the table. All the table data is clean, but the total isn't. So I want to reset it.

The code:

var a = 0;
$(document).ready(function() {
$(document).on("change","#txtInput",function(){
$("#contenido").append("<tr><td>"+$("#txtInput").val()+"</td></tr>");
a += 1;
var str = 'Total Bianuales: '
$('p').html(str + a);
})
});

$(document).ready(function() {
$(document).on("click", "#btnRecibo", function(){
$("#tablaB > tbody").html("");
var str = 'Total Bianuales: '
$('p').html(str + "");
})
});

Answer

The other variables are "reset" because they're in the anonymous functions you have there. a is in the outer scope, so it's available throughout. If you're using it later, you can set it to 0 in your #btnRecibo onclick. If you're not, you could unset it with delete, but that's bad practice and there's no need if you're not using it anymore since it'll be garbage collected automatically.