I use the following to create objects in JavaScript using jQuery:
var article_attrs = {
'class' : 'class-name',
click : function_name,
hover : function_name,
html : $('<h2>', { html : "Heading" })
},
article = $('<article>', atricle_attrs);
This works great - by setting the html attribute I set the content of the article.
What I want to do at this point though, is add a <h2> tag, and a <div> tag including a <footer> tag. My outline for this element will look like:
- article
- h2
- div
- footer
What would be the best way of doing this? (Note this is an example only).
Update: The main question is, using the html attribute, how do I add multiple elements?
I have tried a method from a similar question, but it does not work in this case:
html : [ $('<h2>', { html : "Heading" }), $('<div>', { html : "Some content" }) ]
I have made this fiddle which works.. but my JS still doesn't. I'm definitely missing something.
Edit: Works with jQuery 1.8.* only
$("<element1>").add("<element2>")might do the trick.add()adds another element to the jQuery selection, I think the question is 'how do I append am element to a newly-created jQuery object?'