jQuery: Create Parent for Element

Using jQuery, how can I put #world INSIDE #hello?

I want to create a div with an id of "hello" around this:

<p id="world"></p>

Open in new window

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {
 alert('Create div with id of hello and place #world inside it');
 //  <div id="hello"><p id="world">Foobar/p></div>
});
</script>

</head>
<body>

<p id="world">Foobar/p>

</body>
</html>

Open in new window

LVL 16
hankknightAsked:
Who is Participating?
 
varontronConnect With a Mentor Commented:
Use the wrap function:
$('#world').wrap('<div id="hello" />');

Open in new window


See the documentation here:

http://api.jquery.com/wrap/

cheers,
Dave
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.