Crop elements with Javascript

Asked

Viewed 40 times

-1

I’m having trouble moving elements on the screen with pure javascript. I need to "crop" the element of a place -> And send this element to another place , and do this with all the html that this element contains within it

Is there a way to cut the element from one location and send it to another? This is what I’ve tried to do:

bemVindo.classList.remove('bemVindo'); novoElemento.classList.add('novoElemento') 

but that way it only changes the class, and I need it to be "moved" to the other class.

  • "Moved" what you say is to cut an element of Header and paste in Footer? Your question was not very clear...

  • Yes, cut the element from one place and "paste" into another

1 answer

0

If I understand the question correctly, using appendChild you get what you need.

const f = () => {
document.getElementById('destino').appendChild(document.getElementById('elemento'))
}
#origem {
background-color: red;}

#destino {
background-color: yellow;}

#origem,#destino {height: 60px;
width: 60px;}
<div style="display: flex; justify-content: space-around">
<div id="origem">
<div id="elemento"><a href="#">link</a></div>
</div>
<div id="destino"></div>
</div>

<input type="button" value="mover" onclick="f()" />

  • Perfect, this is exactly what I needed, with few adjustments I solved my situation.. Thank you very much

Browser other questions tagged

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