javascript - Dynamic DOM Manipulation Function: How To Improve It, Should I Be Using It, & What's It Doing Here? -
javascript - Dynamic DOM Manipulation Function: How To Improve It, Should I Be Using It, & What's It Doing Here? -
okay, have thorough understanding of programming (bachelors in cs), web development absolute nightmare, , no 1 i've asked can give me real info on should doing here. of them go, "oh, @ that. nifty." , that's end of it.
i've been doing lot of pet projects dynamic content, , speed process up, wrote function create , insert elements dom. it's straightforward, , not elegant, gets job done.
as title says, i'm looking ways improve this, perhaps improve established practice, , have question implementing it.
here's code:
app.newdomobject = function (type, attributes, values, parentnode, innerhtml) { if (type) { var element = document.createelement(type); if (attributes && values) { if (attributes.length == values.length && attributes.length > 0) { (var = 0; < attributes.length; i++) { element.setattribute(attributes[i], values[i]); } } } if (innerhtml) { element.innerhtml = innerhtml; } if (parentnode) { parentnode.appendchild(element); } homecoming element; } } and when want utilize it, either:
var thing = app.newdomobject("div", ["id"], ["thing"], document.body, "thing1"); or just
app.newdomobject("div", ["id"], ["thing"], document.body, "thing2"); depending on if need reference later or not.
my lastly question is: happens returned element thing2, since i'm not assigning variable? should know this, cluttering global? know i'm not using it, , drop it? there's no prototyping or anything, , it's returning element that's in dom. go , why work without error? :/
i'm sure should read book on advanced javascript mechanics, don't know i'm asking, , 5 minutes googling keywords "expression" , "declaration" , "returning not variable" helpful can imagine.
and posts did find on issue recommended templating libraries, i'd avoid them if can.
any thoughts much appreciated!
thanks help! -tyler
not storing returned value has similar effects in other languages, don't maintain anywhere , in cases it's gone.
in case persisted anyway, since append parameter parentnode, more or less depends on parentnode came , after that.
in illustration function calls append document body, means persisted in dom of current document / website. don't need hold reference utilize later, traverse dom , fetch new reference node.
for inspiration regarding dom manipulation i'd recommend taking @ jquery framework , manipulation api section.
javascript dom dynamic scope
Comments
Post a Comment