9

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

Dmitry Shvedov
  • 2,979
  • 4
  • 36
  • 48
user3438415
  • 155
  • 2
  • 2
  • 10
  • 1
    This code is already jQuery compatible because jQuery is written in JavaScript. – Halcyon Apr 03 '14 at 16:37
  • 1
    And, this is an obvious ducplicate. Did you even google it ? http://stackoverflow.com/questions/268490/jquery-document-createelement-equivalent . @Doge > From what I read I think it's bad practice to mix vanilla JS and jQuery though – Laurent S. Apr 03 '14 at 16:38
  • 1
    `jQuery('')` creates a new `input` element and returns its jQuery object reference – Arun P Johny Apr 03 '14 at 16:39
  • $('
    '); you need this.
    – Jai Apr 03 '14 at 16:41

6 Answers6

6

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

}
hsz
  • 143,040
  • 58
  • 252
  • 308
1
// $("#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 
codefreaK
  • 3,472
  • 4
  • 32
  • 63
0
$('<input />').attr('type','radio').appendTo(document.body)

some like that

Vasiliy Vanchuk
  • 6,618
  • 2
  • 19
  • 43
0

Are you just wanting to create new elements on the fly? If so this should do what you need:

$('<input>').attr({
    type: 'hidden',
    id: 'foo',
    name: 'bar'
}).appendTo('#questions');

Obviously change the "type", and "name" to whatever you need

Andrew Newby
  • 4,563
  • 4
  • 33
  • 70
0

Fiddle Demo

function generateInputs() {
    var i = $("<input/>"); //var i = document.createElement("input");
    for (var j = 0; j < 4; j++) {
        var c = $("<input/>", { //document.createElement("input");
            type: "radio" //r.setAttribute('type',"radio");
        });
        var r = $("<input/>", { //document.createElement("input");
            type: "radio" //c.setAttribute('type',"radio");
        });
        $('#questions').append(r);//document.getElementById('questions').appendChild(r);
        $('#questions').append(c);//document.getElementById('questions').appendChild(c);
    }
    i.attr('type', "text"); // i.setAttribute('type',"text");
    $('#questions').append(i);//document.getElementById('questions').appendChild(i);
}


A little shorter code

Fiddle Demo

function generateInputs() {
    var i = $("<input/>", {
        type: "text"
    });
    for (var j = 0; j < 4; j++) {
        var c = $("<input/>", {
            type: "radio"
        });
        var r = $("<input/>", {
            type: "radio"
        });
        $('#questions').append(r, c);
    }
    $('#questions').append(i);
}

Learn jQuery

jQuery API Documentation

Tushar Gupta - curioustushar
  • 56,454
  • 22
  • 99
  • 107
0
function generateInputs() {
    var i = $('<input type="text"');
    for ( var j = 0; j < 4; j++ ) {
        var c = $('<input type="input" />');
        var r = $('<input type="radio" />');
        $('#questions').append(r).append(c);
    }
    $(document).append(i);
}
Data Crusader
  • 416
  • 1
  • 4
  • 13