javascript - Created element is undefined -
javascript - Created element is undefined -
i'm trying create html5 video volume manipulation, , want insert dom using javascript, when browse on element using for loop, returns undefined. code:
// volume var volumewrapper = document.createelement('span'); volumewrapper.classname = "volumewrapper"; insertafter(elapsetimewrapper, volumewrapper); ( var = 0; <= 4; ++ ) { var volumecontrol = document.createelement('span'); volumewrapper.appendchild(volumecontrol); (function(index) { volumecontrol.onclick = function() { if ( this.classname == "" ) { this.classname = "active"; ( var j = 0; j <= index; j ++ ) { console.log(volumecontrol[j]) // homecoming undefined } } else { this.classname = ""; } } })(i); } i don't know what's wrong code, when insert straight html, it's not undefined. returns exact value of elments. want insert using javascript not html. need help. give thanks in advanced reply.
the construction of code incorrect.
here might help you:
var volumecontrol = []; ( var = 0; <= 4; i++ ) { volumecontrol[i] = document.createelement('button'); volumecontrol[i].innerhtml = i; volumecontrol[i].id = "b" + i; volumewrapper.appendchild(volumecontrol[i]); (function(index) { volumecontrol[i].onclick = function() { if ( this.classname == "" ) { this.classname = "active"; console.log(volumecontrol[index].id); // homecoming undefined } else { this.classname = ""; } } })(i); } i replace span button can understand more going on. when click button, displays id of clicked button. of course, code not want, code construction should help you.
additionnally, insertafterat origin of code not correct.
javascript html5 dom
Comments
Post a Comment