Marcos Henrique Marcos Henrique - 2 months ago 7
Ajax Question

How to Creating html tags each with their ID via a button with JavaScript?

The thing is, I have a button in html to click it executes a JavaScript function that creates a new field with id in a hidden div .
I wanted each new field ( tag ) has created its " ID " specifies , not an ID for everyone as is my code (below) ... I need to be different to record in the database that is entered in each.
Can you help me ?



$(document).ready(function () {
$('#addpergunta').click(function () {
var x = 1;
var div = $('#perg-box');
var divtext = $('<div />');
var text = $('<select />');
text.attr('class', 'form-control').attr('id', 'id-pergunta' + x);
divtext.append(text);
div.append(divtext);
$('#div_perg').show();
});
});




Answer

Here:

$(document).ready(function () {
    var count = 1
    $('#addpergunta').click(function () {
        var div = $('#perg-box');
        var divtext = $('<div />');
        var text = $('<select />');
        text.attr('class', 'form-control').attr('id', 'id-pergunta' + count);
        divtext.append(text);
        div.append(divtext);
        count++;
        $('#div_perg').show();
    });
});