skip to Main Content

I’m trying to grab some simple html and put into another DIV, but all i get back is [object HTMLDivElement], What am i doing wrong here?

this.inner = document.querySelector('div[data-v="1"]')
this.tail = document.querySelector('[data-tail]')
this.tail.innerHTML = this.inner
<div data-tail></div>
<div data-v="1">some content</div>

3

Answers


  1. Add .innerHTML to get the content.

    document.querySelector('div[data-v="1"]') refers to the object

    this.inner = document.querySelector('div[data-v="1"]').innerHTML
    this.tail = document.querySelector('[data-tail]')
    this.tail.innerHTML = this.inner
    <div data-tail></div>
    <div data-v="1">some content</div>
    Login or Signup to reply.
  2. Check this documentation first, The Document method querySelector() returns the first Element within the document that matches the specified selector, or group of selectors. If no matches are found, null is returned.
    You just have to use innerHTML properly

    this.inner = document.querySelector('div[data-v="1"]');
    this.tail = document.querySelector('[data-tail]');
    this.tail.innerHTML = this.inner.innerHTML;
    
    Login or Signup to reply.
  3. Change it like this

    this.tail.innerHTML = this.inner.innerHTML
    
    

    obviously you can not assign a ElementNode as HTML string.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search