Reputation: 2577
I am trying to create a div that will only be acknowledged by JS. I want the div placed over my "container" div id tag like so:
$('<div id="disco">').prependTo('#container');
$('</div>').appendTo('#container');
This does not seem to work however. I was wondering why, and how best to resolve this.
Upvotes: 1
Views: 89
Reputation: 40639
Select your selectors
like <div id="disco" />
or <div id="disco"></div>
instead of <div id="disco">
and select <div/>
in place of </div>
$('<div id="disco"></div>').prependTo('#container');
$('<div/>').appendTo('#container');
See Demo
If you want to insert disco div before container then use insertBefore() or before() like,
$("<div id='disco'></div>").insertBefore("#container");
And if you want to wrap
your #container
with disco div
then use wrap() as Palash and Kristof Feys answered like,
$( "#container" ).wrap( "<div id='disco'></div>" );
Upvotes: 1
Reputation: 73896
Since you want the div placed over the container
div, you can do this using .wrap()
:
$( "#container" ).wrap( "<div id='disco'></div>" );
Consider the following HTML:
<div id="container">Hello</div>
The result after using wrap
is a new wrapped around the matched element:
<div id="disco">
<div id="container">Hello</div>
</div>
Upvotes: 0
Reputation: 1842
My guess is that you are trying to wrap a new div arround your container? If this is the case, you can use the following (jQuery) code, since you are already using jQuery in your example:
$('#container').wrap('<div id="disco"></div>');
cfr. http://api.jquery.com/wrap/
Upvotes: 1