Вопрос:

Moving a child from one parent to another in JavaScript?

javascript jquery html parent-child

359 просмотра

2 ответа

20 Репутация автора

I would like to be able to change an elements parent. Basically I would like to move an element from one parent to another. I would like this to happen in Javascript, preferably using only the JQuery library.

Before code:

<div id = "div1">
  <p id = "paragraph">
    Lorem ipsum dolor sit amet, adipiscing pellentesque egestas.
  </p>
</div> 
<div id = "div2></div>

After code:

<div id = "div1"> </div> 
<div id = "div2>
  <p id = "paragraph">
    Lorem ipsum dolor sit amet, adipiscing pellentesque egestas.
  </p>
</div>
Автор: Abishek Roka Источник Размещён: 08.11.2017 10:23

Ответы (2)


0 плюса

1722 Репутация автора

$('#paragraph').appendTo('#div2')
Автор: John Ellmore Размещён: 08.11.2017 10:25

0 плюса

28 Репутация автора

$('#div2').append('#paragraph'); Should do it!

Just a little side note: Use .append() when you add the element as the last children of the parent(#div2). If you want it as the first you can use .prepend()

Автор: Rarex Размещён: 09.11.2017 01:05
Вопросы из категории :
32x32