1

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.

4
  • use before api.jquery.com/before instead of prependTo or what do you mean with placed over? Commented Nov 6, 2013 at 7:43
  • Basically I want the 'disco div' to blanket over 'div container', like a, well, container. I'm looking at the before(), but it doesn't wrap around my 'div container'. Commented Nov 6, 2013 at 7:49
  • so use .wrap() instead Commented Nov 6, 2013 at 7:50
  • thanks. worked like a charm. Commented Nov 6, 2013 at 7:55

4 Answers 4

1

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/

Sign up to request clarification or add additional context in comments.

Comments

1

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>" );

3 Comments

your "insertBefore" example needs single quotes :)
@KristofFeys : disagree
i meant the ones you've changed ... double around div and double around disco won't work. But you've changed them already :D
0

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>

Comments

0

Try it:

$('#container').wrap('<div id="disco"></div>');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.