April 24, 2012

put div and inside <td> tag

Question by vikas tyagi

it’s dynamic code and i have design issue with that

<td id="test" class="test1"></td>
<div id="WidgetButton" class="right">test</div> // that part of code making dyamic


  there is any way put that <div> has id WidgetButton and inside <td></td>

like that

<td id="test" class="test1">
<div id="WidgetButton" class="right">test</div>
</td>

plz suggest me how can i do it with jquery

thanks

Answer by T.J. Crowder

As both of them have ids, this is a trivial application of the jQuery function (usually aliased as $) and appendTo.

$("#WidgetButton").appendTo("#test");

It’s just as well they both have ids, too, because otherwise it would be tricky, since the markup is invalid and so browsers will relocate the div to where they guess it should go, so using structural selectors and relative traversal would be difficult. The id values trump that, thankfully.

Answer by Starx

Update:

Since you only want to do this for div with id WidgetButton. Heres how

 $("#WidgetButton").appendTo("#test");

If you to remove all divs and put inside a td, it might prove to be dangerous. But you can do this like

$("div").each(function() {
    $(this).appendTo("#test");
});

However, I will extremely recommend you narrow your selection to a particular group of divs like using class names or ids.

ClassName

$(".divswithclassname").each(function() {
    $(this).appendTo("#test");
});

Certain Id

$("#idofdiv")..appendTo("#test");

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!