Javascript: Failed to execute 'removeChild' on 'Node': parameter 1 is not of type 'Node' -
i trying recreate practices 1 of courses. remove li-item ul , append ul.
when write code in following way works finde
var removemeandappendme = function() { var parentli = this.parentnode; var goneelement = incomplelist.removechild(parentli); complelist.appendchild(goneelement); }; var li = incomplelist.getelementsbytagname('li'); (var = 0; < incomplelist.children.length; i++) { var link = li[i]; var licheckarray = link.getelementsbytagname('input'); var licheck = licheckarray[0]; licheck.onchange = removemeandappendme; }
when change code following error "failed execute 'removechild' on 'node': parameter 1 not of type 'node'".
function removemeandappendme(fromlist, tolist) { var parentli = this.parentnode; var goneelement = fromlist.removechild(parentli); tolist.appendchild(goneelement); } var li = incomplelist.getelementsbytagname('li'); (var = 0; < incomplelist.children.length; i++) { var link = li[i]; var licheckarray = link.getelementsbytagname('input'); var licheck = licheckarray[0]; licheck.onchange = removemeandappendme(incomplelist, complelist); }
what bothers me, fact code runs when removemeandappendme-function without parameters , doesnt work parameters. can tell why , mistake is? thank you.
(i'm aware of blur-problem discussed here: failed execute 'removechild' on 'node')
first, pointy mentioned, need wrap call removemeandappendme(incomplelist, complelist)
in anonymous function not invoked prematurely.
taking account, receiving error because of value of this
case of each function invocation. when calling removemeandappendme()
, this
htmlinputelement object, when calling removemeandappendme(incomplelist, complelist)
, this
window object, , this.parentnode
undefined
(and "not of type 'node'", why you're seeing error message).
there lot of subtleties question: this
refers to, , how different 'function' declarations treated (lots of discussion here). changing way removemeandappendme(incomplelist, complelist)
declared doesn't resolve issue either.
in way, question boils down "why this
refer window object parameterized function call, htmlinputelement object non-parameterized function call?" believe in case, because, when wrap invocation of parameterized function call in anonymous function (like so: licheck.onchange = function(){removemeandappendme(incomplelist, complelist);};
), removemeandappendme
has no 'local' owner, ownership of function defaults global object, window (reference).
to fix this, can pass in this
invocation of removemeandappendme
, this
refer checkbox, , use variable within parameterized function. i've put of in fiddle things can played commenting/uncommenting different things. hope helped.
Comments
Post a Comment