0
I’m trying to print with appendchild, without having to mount using Document.createelement, I know it’s possible but I don’t know how, with innerHTML works but it erases everything, follows below:
var div = "<div id='teste'>valorqualquer</div>";
document.getElementsByTagName("body")[0].appendChild(div);
//But present the message below:
Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.
I’ve tried that (but it only prints text):
div = document.createTextNode(div);
document.getElementsByTagName("body")[0].appendChild(div);
Look at the structure I need to build:
<div id="erroDeCampos" class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel">
<div class="modal-dialog modal-sm" role="document">
<h3 class="center">Campos com erro:</h3>
<div class="alert alert-danger">
<strong>Erro!</strong> You should <a href="#" class="alert-link">read this message</a>.
</div>
</div>
</div>
For each field that my form present will be created this structure, but to do this I think would be a very large code, follows part of the code:
var divElement = document.createElement("div");
divElement.setAttribute("id", "erroDeCampos");
divElement.setAttribute("class","modal fade bs-example-modal-sm");
divElement.setAttribute("tabindex", "-1");
divElement.setAttribute("role", "dialog");
divElement.setAttribute("aria-labelledby", "mySmallModalLabel");
var divElementModalAninhado = document.createElement("div");
divElementModalAninhado.setAttribute("class", "modal-dialog modal-sm");
divElementModalAninhado.setAttribute("role", "document");
var h3ElementAninhado = document.createElement("div");
h3ElementAninhado.setAttribute("class", "center");
h3ElementAninhado.innerHTML = "Campos com erro";
var divaninhadoaoh3acima = document.createElement("div");
The right way is with
document.createElement
. Why don’t you want to wear this?– bfavaretto
because there are several elements within each other and with very distinct values if I replace only a few values and keep the structure will already be enough for me to put inside one be along with the changes I need.
– rock.ownar
Set a more precise example of your situation and explain better what you are trying to do.
– bfavaretto