tags:

views:

30

answers:

2

I'm trying to construct the following div element using jQuery:

<div>
    <p>Hello</p>
    <p>Stack Overflow</p>
</div>

I added the first paragraph like this:

$('<div/>', {html: $('<p/>', {text: "Hello"})})

but I don't know how could I add the second paragraph.

Please help.

+1  A: 

What's wrong with doing it in several steps?

var div = $('<div />');

var p1 = $('<p />');
p1.text('hello');
p1.appendTo(div);

var p2 = $('<p />');
p2.text('stack overflow');
p2.appendTo(div);
kijin
Nothing wrong. I just wonder if it is achievable using the `jQuery(html, props)` syntax mentioned here: http://api.jquery.com/jQuery/#jQuery-html-props
Misha Moroshko
It seems you can't add two elements using the props syntax unless you manually write the HTML for those two. You can't use `+` to concatenate two elements, because they're both jQuery objects. The closest you can get is `$('<div/>', {html: $('<p/>', {text: "Hello"})}).append($('<p/>', {text: "Stack Overflow"}))`
kijin
OK, thanks a lot!!
Misha Moroshko
A: 

Try the following

var d = $('<div/>')
var all = ["hello", "stackoverflow"];
for (var i in all ) {
    var p = $('<p/>');
    p.text(all[i]);
    p.appendTo(d);
}
JaredPar