Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Element.insertAdjacentHTML() API throws error in chrome 55.0.2883.87

I found the explaination about the API here, which tells me the second parameter is a string.

It's perform normal in firefox. However, in chrome, it shows the errors below:

errors about <code>Element.insertAdjacentHTML() API</code> in chrome

I just curious about what makes this? Is it because the api is still stay Working Draft status and different browsers do the different implementation?

The following is my code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <div id="div">
        <ul>
            <li>xxx</li>
            <li>xxx</li>
            <li>xxx</li>
            <li>xxx</li>
            <li>xxx</li>
        </ul>
    </div>
    <script>
    //normal
    // var pTag=document.createElement("p");
    // div.insertAdjacentElement("beforeend",pTag);

    //throw error:
    // Uncaught TypeError: Failed to execute 'insertAdjacentElement' on 'Element': parameter 2 is not of type 'Element'.
    var txt = "<p>testtest</p>";
    div.insertAdjacentElement("beforeend",txt);
    </script>
</body>
</html>
like image 342
PageYe Avatar asked Mar 06 '17 14:03

PageYe


People also ask

How does insertAdjacentHTML work?

The insertAdjacentHTML() method of the Element interface parses the specified text as HTML or XML and inserts the resulting nodes into the DOM tree at a specified position.

How do you use insertAdjacentElement?

When one is clicked, it becomes selected and you can then press the Insert before and Insert after buttons to insert new divs before or after the selected element using insertAdjacentElement() .

What is adjacent HTML?

Definition and Usage The insertAdjacentHTML() method inserts HTML code into a specified position. Legal positions: Value.


2 Answers

Just Change div.insertAdjacentElement to div.insertAdjacentHTML:

<!-- <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Coding revoltion</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>

    <div class="parent">
        <ul class="ul"></ul>
    </div>



    <script src="dom.js"></script>

</body>
</html>

 -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <div id="div">
        <ul>
            <li>xxx</li>
            <li>xxx</li>
            <li>xxx</li>
            <li>xxx</li>
            <li>xxx</li>
        </ul>
    </div>
    <script>
    //normal
    // var pTag=document.createElement("p");
    // div.insertAdjacentElement("beforeend",pTag);

    //throw error:
    // Uncaught TypeError: Failed to execute 'insertAdjacentElement' on 'Element': parameter 2 is not of type 'Element'.
    var txt = "<p>testtest</p>";
    div.insertAdjacentHTML("beforeend",txt);
    </script>
</body>
</html>
like image 114
Rohit-Pandey Avatar answered Sep 17 '22 14:09

Rohit-Pandey


The parameters are incorrect. The second parameter should be html element, in your case it's a string.

like image 36
Sharon Haim Pour Avatar answered Sep 16 '22 14:09

Sharon Haim Pour