Accountant Jquery

Asked

Viewed 30 times

1

Good evening, could someone assist me in a dubious.

Each time the person clicks on the BUTTON is called the function:

function cloneDivDescricaoProduto() {
    var count  = 0;
    count++;
    console.log(count);
    var html_corpo = '<div class="form-group col-md-3"><label for="descricao">Qual seria o tamanho ?</label><input name="data[Tamanho]['+ count+'][descricao]" class="form-control" id="descricao" placeholder="Ex: Pequena, Media, Grande" type="text"></div>'+$(".criarCloneDescricao").html()+'</div>'
    $(".criarCloneDescricao").html(html_corpo);
}

And in this function when being called I would like you to run the counter but always when you click it returns me 1 on the console. What would be the expected result: By clicking the button call the function cloneDivDescricaoProduto() and the counter is incrementing with each click and adding in the counter variable in the created div.

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

1 answer

0


In itself, you every time you call the function are creating the counter variable of 0. You are not saving the value of this variable outside the scope of the function.

You can solve this problem by creating the Count variable outside of this function, so every time the function is executed the variable will not be recreated

var count  = 0;

function cloneDivDescricaoProduto() {
 count++;
 console.log(count);
 var html_corpo = '<div class="form-group col-md-3"><label for="descricao">Qual seria o tamanho ?</label><input name="data[Tamanho]['+ count+'][descricao]" class="form-control" id="descricao" placeholder="Ex: Pequena, Media, Grande" type="text"></div>'+$(".criarCloneDescricao").html()+'</div>'
 $(".criarCloneDescricao").html(html_corpo);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.