user3438415 user3438415 - 14 days ago 10
Javascript Question

createElement in jQuery

I work with CreateElemant in JavaScript this is my code:

function generateInputs()
{
var i = document.createElement("input");
for(var j=0;j<4;j++)
{
var c = document.createElement("input");
var r = document.createElement("input");
r.setAttribute('type',"radio");
document.getElementById('questions').appendChild(r);
c.setAttribute('type',"input");
document.getElementById('questions').appendChild(c);
}

i.setAttribute('type',"text");

document.getElementById('questions').appendChild(i);

}


And I want to write it with jQuery but I didn't find an equivalent for createElement()

hsz hsz
Answer

Just try with:

function generateInputs()
{
    var i = $('<input type="text"/>');

    for(var j=0;j<4;j++)
    {
        var c = $('<input type="text"/>');
        var r = $('<input type="radio"/>');

        $('#questions').append(c).append(r);
    }


    $('#questions').append(i);

}
Comments