Lets say that I have a element reference obtained ala…
var x=document.getElementById("ID123");
Now I want to include some function of x on a dynamically created html element ala…
myWin.document.write("<input type='button' data-doit=????? value='Do It' onclick='window.doit(?????);'/>");
so that function doit() can recreate the original reference x
function doit()
{
var x = ?????;
}
Right now I’m doing this by obtaining a numeric index of x as found via getElementsByTagName and passing that as a parameter to doit().
Is there some way to do this?
I would probably pass the ID of
xtodoit(), which would requiredoit()to look something like this:As far as creating the
inputelement goes, there a couple of ways to do this. This is similar to your current solution (but non-standard):The standard solution is to create your new
inputelement using the DOM API:(Note that inserting the element is a little more complicated in this situation – you have to programmatically find its location in the DOM).
Finally, if you’re using jQuery, you might do something this: