tags:

views:

1430

answers:

2

I have a simple jQuery question.

How come I can't do this?

$("<div class='flora'></div>").insertBefore('</body>');

I just want to add a new div at the end of the HTML page. So I thought I'd use the .insertBefore method, but jQuery seems to not like to target the </body> tag.

thoughts?

Thanks!

+1  A: 

You should use the .append() method instead.

http://docs.jquery.com/Manipulation/append

"Append content to the inside of every matched element. This operation is the best way to insert elements inside, at the end, of all matched elements. It is similar to doing an appendChild to all the specified elements, adding them into the document."

Jonathan Sampson
Sorry to steal your thunder.
Boushley
Hehe. No problem, man :) Brilliant minds think alike - we both happen to know the answer to that one, and you were rewarded for it, as you should be :)
Jonathan Sampson
+6  A: 

Try something like

$('body').append("<div class='flora'></div>");

And if you really want to append before something... you can use the before() method.

$('something').before("<div class='flora'></div>");
Boushley
You can also use $("<div class='flora'></div>").appendTo('body');
MiffTheFox
Thank Boushley. I found this out only a couple of minutes after posting this.
Loony2nz