Javascript createElement () not working in Chrome

Javascript createElement()does not work in Chrome, but it works fine in IE and Firefox. What for?

+4
source share
5 answers

It works great, use this code:

var catDiv = document.createElement("div");
catDiv.innerHTML = "Test";
document.body.appendChild(catDiv);

Another working example (if you have an element with Id = myTableBody in your HTML)

var appendingTo = document.getElementById("myTableBody");
var tr = document.createElement("tr");
tr.setAttribute("name", "i");
appendingTo.appendChild(tr);
+4
source
var name = document.createElement("Div" ); 

will work. And later you can add attributes like

name.colSpan="2";
document.body.appendChild(name);

Note. Do not try to use angular brackets, for example createElement("<div />"). It will not work in Chrome.

Edit: The syntax problem in the above code has been fixed. instead of a comma there should be a period.

+4
source

, , "createElement":

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

        <title></title>

        <script>
            window.onload = function () {
                for (var i = 0; i < 100; i ++) {
                    var div = document.createElement ("div");
                        div.style.border = "1px solid black";
                        div.style.margin = "20px";
                        div.style.padding = "10px";

                    document.body.appendChild (div);
                }
            }
        </script>

        <style></style>
    </head>

    <body></body>
</html>

enter image description here

+3

createElement() . Caio , , .

w3schools.com , , ex. createElement("DIV"), .

"DIV" "DIV" .

Caio.

+1

. . , createElement, .

0

Source: https://habr.com/ru/post/1789208/


All Articles