A
A
Andrey Frolov2015-10-30 21:00:49
JavaScript
Andrey Frolov, 2015-10-30 21:00:49

Why doesn't replaceWith work?

When executing the code below, it throws an error "Uncaught TypeError: box[i].replaceWith is not a function". Why?

<html>
<head>
  <meta charset="utf-8">
</head>

<body>
  <div class="box">1</div>
  <div class="box">2</div>
  
  <script>
    var a = document.createElement('a');
    a.href = '#';
    a.innerHTML = 'links';
    
    var box = document.getElementsByClassName('box');
    
    for(var i = 0, count = box.length; i < count; i++) {
      box[i].replaceWith(a);
    }
  </script>
</body>
</html>

or in some other way can be replaced <div class="box">1</div>, etc. to the link?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
amatory10, 2015-10-30
@farpram

as I understand replaceWith method refers to Jquery, there is no such method in js.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question