lundi 2 février 2015

document.getelementById doesn't work on dynamically generated divs


Vote count:

0




I am trying to update the content of a dynamically generated div with .innerHTML and getElementById, but when I test it, I get an error: "Uncaught TypeError: Cannot set property 'innerHTML' of null" I also tried using alert() and checking the value of quizDiv at breakpoints. Well, here's my JS file called main.js:



function makeDiv() {
document.body.innerHTML = "<div id='quizDiv'></div>"
}
function createInputBox(boxId) {
var quizDiv = document.getElementById("quizDiv");
quizDiv.innerHTML = "<input type='text' id='" + boxId + "'>";
}
makeDiv();
createInputBox(1);


Here's my html:



<!-- doctype, head, etc -->
<body>
<!-- content -->
<script src="/main.js"></script>
</body>


Any help is appreciated. Thanks!



asked 1 min ago







document.getelementById doesn't work on dynamically generated divs

Aucun commentaire:

Enregistrer un commentaire