What javascript function do I use to extract an HTML block from one place and insert it into another on the same page

Asked

Viewed 418 times

0

Next I’m touching the moodle and 90% of its html is generated via backend, I need to change a <h1 id='courseheader')>Title</h1> from place via javascript since I do not have access to this element, the idea was to remove it from where it is to throw it into a container I have on the same page.

What javascript/Jquery function can I use for this?

I googled but could not express doubt so clearly that could return me answers.

I hope here is clear doubt, anything edits. Thank you.

Example:

<h1 id='courseheader')>Title</h1>

<div class="container"> Quero retirar o h1 ali de cima e colocá-lo aqui dentro</div>

Note: I could even create a new H1 inside the container via JS but I would like to take advantage of what already has.

Obs2.: I forgot to mention, I want that h1together with the other elements inside the container and that it stays inside the tag h1...

  • A bit pedantic but it’s interesting to note that what you want is to change one place element gift .

  • you want a solution using jquery or vanilla JS?

  • In vdd I would like to know the function, using jquery. I even think a question half 'beast' but only needed to know which function to use ... @Caiofelipepereira

2 answers

1

Using jQuery, you have several ways to do this. I don’t know if it’s the most performative or the most elegant, but it solves the problem. See:

var _h1 = $('#courseheader').html();

var _container = $('.container');

setTimeout(function(){
  _container.append(' ' + _h1);  
}, 2000);
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<h1 id='courseheader'>Title</h1>

<div class="container"> Quero retirar o h1 ali de cima e colocá-lo aqui dentro</div>

The setTimeout is a perfumery, just for you to see working.

EDIT

I purposely erased the contents of div because I thought that’s what you wanted. I edited the code to conform to your comment

  • 1

    Cool, and more or less that there I want, in vdd he could not erase the content that is inside the div but just join the other elements, but you have given the way, I’ll tamper with the code to see if I can solve ... Thanks Caio.

  • See the edited response

  • Face only a dumb doubt, what you changed in the code even not to erase the content that was in the container div?

  • 1

    I changed my _container.html('').append(_h1); for _container.append(' ' + _h1);

  • haha vlw onbrigado

1


You can use the code below as an example, it:

  1. Captures the element h1 desired through the ID
  2. Captures the element container through the className
  3. Adds to the textContent container a new clone element of h1 through the method cloneNode(true)

Note: In order to avoid line breaking in the container, it was necessary to add the property display: inline;

//Recuperando elemento H1 
var h1Element = document.getElementById("courseheader");

//Recuperando elemento container
var containerElement = document.getElementsByClassName('container')[0];

//Adicionando conteúdo do h1 ao container
containerElement.appendChild(h1Element.cloneNode(true));
h1{
  display: inline;
 }
<h1 id='courseheader'>Title</h1>

<div class="container"> Quero retirar o h1 ali de cima e colocá-lo aqui dentro</div>

  • I edited my question I think I forgot to mention that I need H1 to stay inside the container and not erase the other elements that exist there and join them.

  • @Erick Updated response

  • 1

    +1 for using pure JS

  • Nice to stir here to put it on top =D ... But thanks to the two who helped, that’s where I really wanted... vlwww

  • 1

    The last line of the code I put containerElement.insertBefore(h1Element.cloneNode(true) containerElement.firstchild); and the element came first =D

Browser other questions tagged

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