I wanted to make simple to-do list app, i created function which gets value from input box and inside this function as wells creates checkbox near text, now i want to make function which will executes whenever checkbox is being clicked. Since checkbox is not on the page when documents load i'm getting error, how can i make it work properly?
Here is my code:
var btn = window.document.getElementById("btn");
var result = window.document.getElementById("result");
function getValue() {
var input = window.document.getElementById("input").value;
var checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.className = 'checkbox';
result.appendChild(checkbox);
result.innerHTML += input + "<br>";
}
var checkbox = document.getElementById('checkbox');
btn.addEventListener('click', getValue);
checkbox.addEventListener('click', function(){
alert("Clicked");
});
<input type="text" id="input">
<input type="button" value="Submit" id="btn">
<div id="result">
</div>