Change widget position with Javascript

Asked

Viewed 90 times

-1

Currently, the element is like this :

<li>
  <a>
    <i> </i>
  </a>
</li>

But, I want to change the tag position i with Javascript, out of the tag to, making the tags sisters, ie :

<li>
  <a> </a>
  <i> </i>
</li>

I have tried this with insertAdjacentHTML, but something is going wrong, because I want to take the father element, and so I’m not getting it

  • 1

    If I understand correctly, just take the element a and make a li.appendChild(a). The element i will be moved to the end of the li.

  • Thank you!!!!!!!!

1 answer

1


I added an id to the li, to make the example easier:

HTML:

<li id="li">
  <a>
    <i>a</i>
  </a>
</li>

JS:

//Salva os nodes em variaveis
var li = document.getElementById("li"); 
var a = li.firstElementChild;
var i = a.firstElementChild;

li.prepend(i); //adiciona o <i> na li
a.removeChild(i);// remove o <i> do <a>

Fiddle of the example. https://jsfiddle.net/xLyunqw4/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.