L
L
Link-Z2021-07-10 13:04:54
JavaScript
Link-Z, 2021-07-10 13:04:54

Why doesn't cloneNode work?

I need to copy the block and paste it, but I can't figure out why cloneNode doesn't work, it throws an error "cloneBlock.cloneNode is not a function"

HTML

<div class="blocks">
    <div class="block">
      <h1 class="title">Привет 1</h1>
      <p>Lorem ipsum dolor 1</p>
    </div>
  </div>
  <button class="copy">Копировать</button>


JS
let cloneBlock = document.getElementsByClassName('.block');
let parent = document.querySelector('.blocks'); 
let copy = document.querySelector('.copy');

copy.addEventListener('click', function(){
  const exemple = cloneBlock.cloneNode(true);
  parent.append(exemple)
})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AveWycc, 2021-07-10
@Link-Z

getElementsByClassName doesn't need a dot. Due to the fact that cloneBlock is empty (undefined), an error occurred.
Write AND, for the future, do not mix querySelector with the rest, otherwise you will also get confused.
getElementsByClassName('block');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question