How to wrap an element as parent of a known element using GQuery

404 views Asked by At

Say I have a div as:

<div id="myDiv"> </div>

I have a GQuery object which I got using

GQuery mydiv = GQuery.$("#myDiv");

Using this, I want to create a new parent which wraps this div element inside it. For example, if the parent is another div, following is what I want:

<div id = "parentDiv"> 
   <div id="myDiv"> </div>
</div>

Though this sounds like a simple thing to do, I am not able to get the desired result.

Note: I have tagged JQuery for this question as well since if a simple method for the same exists in JQuery, it is probable that it would exist in GQuery as well.

2

There are 2 answers

1
Wirone On

I think you want use wrap():

GQuery mydiv = GQuery.$("#myDiv").wrap('<div id="parentDiv" />');

I don't know GQuery, it's based on jQuery.


I read some documentation and I'm not sure if your syntax should be:

GQuery mydiv = $("#myDiv").wrap('<div id="parentDiv" />');

or something else. Just be sure you have good syntax and use wrap().

3
U.P On

In jQuery it is like this

$(document).ready(function(){
var parent = $("#myDiv").parent();
$("<div id='parentDiv'></div>").appendTo(parent)
                  $("#myDiv").appendTo($("#parentDiv"));
});​

see http://jsfiddle.net/fZ6zx/