Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

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

0
hankknight
Asked:
hankknight
1 Solution
 
varontronCommented:
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now