0
In my code I made a function to create a button and right after the user click on it the button and the other altered content are deleted.
I managed to reach that point, but button is at the end of body
. To solve this I needed to use the function elementoPai.appendChild(node);
, what would be the following result in my code:
var pai = document.getElementById("form1");
var b1fechar = document.createElement("BUTTON");
b1fechar.setAttribute("id", "b1fechar");
b1fechar = document.getElementById("b1fechar");
pai.appendChild(b1fechar);
That would be the interesting part of the code.
Whole javascript down here (file . js):
var pai = document.getElementById("form1");
//Funções básicas de texto para output visual no site
function GetMoveInfo()
{
var ictext = document.getElementById("ictext").innerHTML = iceBeam.info;
var b1fechar = document.createElement("BUTTON");
b1fechar.setAttribute("id", "b1fechar");
b1fechar.addEventListener("click", closeIcebeam, true);
b1fechar.innerHTML = "Fechar";
pai.appendChild(b1fechar);
//document.createTextNode(""); para criar texto para outras ocasiões.
};
Everything works up to line 8.
HTML of the site:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Em construção</title>
<script type="text/javascript"></script>
</head>
<body>
<form id="form1">
<input type="radio" id="icebeam" name="mov" value="icebeam">
<label for="icebeam" >Icebeam</label><br>
<button type="button" onclick="GetMoveInfo()" id="showicebeam">+Informações</button><br>
<p id="ictext"></p>
<br>
<input type="radio" id="flamethrower" name="mov" value="flamethrower">
<label for="icebeam">Flamethrower</label><br>
</form>
</body>
</html>
I accept suggestions for code improvement too, obg.
– user216621
b1fechar = document.getElementById("b1fechar");
pq assigned a value to an element that already had value, and hasn’t even been added to the DOM? this will result in null, thegetElementById
will read documents that are part of the DOM, and yours has not yet been inserted in this example. Other than that, in FunctionGetMoveInfo()
, where was the "parent" variable set? Take advantage and remove the tagjava
, pq this code has nothing of java, usejavascript
– Ricardo Pontual
@Ricardopunctual does not have the java tag... It just has the javascript tag. I will review this error you commented, obg.
– user216621