0

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.

Saurabh Agarwal
  • 507
  • 2
  • 5
  • 16

2 Answers2

5

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().

Wirone
  • 3,304
  • 1
  • 29
  • 48
0

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/

U.P
  • 7,357
  • 7
  • 39
  • 61
  • You don't know if `#myDiv` has parent because Saurabh didn't say anything about that. Maybe it's a dynamically created div. Your code in my opinion is triumph of form over content - maybe it would work, but it's not optimal.. – Wirone Jun 05 '12 at 13:14
  • If there is no other parent then is its parent. See the jsFiddle, it works. Though, your solution looks better. Just up voted your answer. – U.P Jun 05 '12 at 13:21
  • `body` is a parent when `#myDiv` is present in DOM, but if you use `myDiv = $('
    ');` and don't append it anywhere, there is no parent.
    – Wirone Jun 05 '12 at 13:26