Trying to call a function on the created link. But the alert only shows when click on the Top Level.
Here is my js:
<script>
var theUL, theLi, theA;
function test() {
mystart = document.getElementById('testdiv');
for (var i=0; i<4; i++) {
theLi = document.createElement("li");
theA = document.createElement("a");
theA.id = "aid";
theLi.appendChild(theA);
mystart.appendChild(theLi);
theA.innerHTML = "test" + i;
theA.href = " + i;
document.getElementById('aid').onclick = alert('in'); //click on the each new link need to call a js function
}
}
</script>
My HTML:
<a href="#" onclick="test();">Top Level: </a><br>
<div id="testdiv"></div>
Thanks!
Here is my js:
<script>
var theUL, theLi, theA;
function test() {
mystart = document.getElementById('testdiv');
for (var i=0; i<4; i++) {
theLi = document.createElement("li");
theA = document.createElement("a");
theA.id = "aid";
theLi.appendChild(theA);
mystart.appendChild(theLi);
theA.innerHTML = "test" + i;
theA.href = " + i;
document.getElementById('aid').onclick = alert('in'); //click on the each new link need to call a js function
}
}
</script>
My HTML:
<a href="#" onclick="test();">Top Level: </a><br>
<div id="testdiv"></div>
Thanks!