Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

createElement in jQuery [duplicate]

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()

like image 553
user3438415 Avatar asked Apr 03 '14 16:04

user3438415


2 Answers

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);

}
like image 167
hsz Avatar answered Oct 06 '22 00:10

hsz


// $("#id"), $("element") or $(".class") for selecting parent


$("#foo").append("<div>hello world</div>")

var txt1="<p>Text.</p>";               // Create element with HTML  
var txt2=$("<p></p>").text("Text.");   // Create with jQuery
var txt3=document.createElement("p");  // Create with DOM
txt3.innerHTML="Text.";
$("p").append(txt1,txt2,txt3);         // Append the new elements 
like image 33
codefreaK Avatar answered Oct 06 '22 00:10

codefreaK