views:

36

answers:

4

I want to create the following DOM structure using jQuery.

<li>
  <label> user
    <input type=radio>
  </label>
</li>

Here is my jQuery.

$('<input>')
.attr({type:'radio'})
.appendTo($('<label>'))
.text('user')
.appendTo($('<li>'))

Some how it's not working or there is something wrong, what would be best practice here?

+3  A: 

your problem is this,

$('<input>')
.attr({type:'radio'}) // until this line, input type radio was created as expected.
.appendTo($('<label>')) // you have created label and append the radio button to it.
.text('user') // the problem here, is .text() is referring to $('<input>'), and not the label.
.appendTo($('<li>')) // and you append the radio in a dynamically created li...

I think you have just missed the logic. Try this,

var $li = $('<li>');
var $label = $('<label>').text('user');
var $radio = $('<input>').attr({type:'radio'});

$label.append($radio).appendTo($li);
$li.appendTo('#ULId');

if you want to chain everything, then do it this way,

$('<label>').text('user')
.append($('<input type="radio">'))
.appendTo($('<li>').appendTo('#someULid'));
Reigel
thats right , i was doing it separate but i was thinking if i can using it by method chaining. thanks for your input
JapanPro
see added notes ^^;
Reigel
something like this but , here again li is not showing up $('<label>').text('user').append($('<input>').attr({type:'radio'})).appendTo($('<li>')).appendTo('body')
JapanPro
after this, `.appendTo($('<li>')) .appendTo('body')`, you would be appending the `label` to the `body`, and not the `li`.
Reigel
i want to append including li to body
JapanPro
see my last example. Just change `#someULid` with `body`.
Reigel
this is the working solution, thanks for your input $('<label>').text('user').append($('<input type="radio">')).appendTo($('<li>').appendTo('body'));
JapanPro
You are most welcome. ;)
Reigel
Thanks geigel,$('<label>').text('user').append($('<input>').attr({type:'radio'})).appendTo($('<li>').appendTo('body'));
JapanPro
A: 

use $('input') instead of $(''), same with label and li,

Nico
A: 

You don't actually have to put the tags in the selector function

for example your code in append should just be appendTo($('li'))

furthermore, it's best if you had id attributes for your elements if you need to manipulate them with jquery DOM otherwise all LI's on your page would be affected when you run that code.

that is more visible on the part where you append it to label, it would be nice if you can just specify appendTo($('li#my_list li').first())

lock
$("<li />") creates a new (un-attached) li element, it does not select any LI nodes in the document.
Sergey
`I want to create the following DOM structure using jQuery.`, from the OP, meaning the elements has to be created by jQuery and not *selecting* it from the `DOM` as I have understand it in your answer. ;)
Reigel
A: 

You can just create the whole thing from a string:

var $mynode = $("<li><label> user <input type=radio></label><li>");

or create several nodes and then combine them:

var $input = $("<input type=radio>");
var $label = $("<label> user </label>");
var $li = $("<li />");

$label.append($input);
$li.append($label);

My point is, your code is chained to the point that it's very hard to read and understand it.

Sergey