I am working on a script to create and remove DOM elements, and I want to make it as efficient as possible (no redundancies). Because DOM elements each have their own set of attributes, the function variable list is quite long, and each type of element may not use some of the variables. As a placeholder, I use "null" when there is no value, but this can result in lots of "null" values. So I want to create a function to autofill the "null" values. Here's what I came up with:
[code=javascript]
// any one of these could be "null" depending on the DOM element being created
function createDOM(dom, parent, id, alt, src, method, type, value, size, name, text, onlcick, pSelect)
{
var d;
var null(d);
var newNull = "null";
for (null(d); d > 0; --d)
{
newNull += ",null";
};
null(d) = newNull;
(...)
}
[/code]
So without the null function, an example createDOM might look like this:
[code=javascript]
createDOM("img" , "divlogin", "loginimg", "Login", "../images/defaultLogin.jp g", null, null, null, null, null, null, null, null, "loginForm( )", page.length - 2);
[/code]
A real pain in the arse, eh? But with the null function, it would look like this instead:
[code=javascript]
createDOM("img" , "divlogin", "loginimg", "Login", "../images/defaultLogin.jp g", "null(8)", "loginForm( )", page.length - 2);
[/code]
Which is much better, but perhaps still not great. Am I on the right track, or am I way off base here? Is there a better way to do this, either in the null function or in the original createDOM() variable list?
Thanks.
[code=javascript]
// any one of these could be "null" depending on the DOM element being created
function createDOM(dom, parent, id, alt, src, method, type, value, size, name, text, onlcick, pSelect)
{
var d;
var null(d);
var newNull = "null";
for (null(d); d > 0; --d)
{
newNull += ",null";
};
null(d) = newNull;
(...)
}
[/code]
So without the null function, an example createDOM might look like this:
[code=javascript]
createDOM("img" , "divlogin", "loginimg", "Login", "../images/defaultLogin.jp g", null, null, null, null, null, null, null, null, "loginForm( )", page.length - 2);
[/code]
A real pain in the arse, eh? But with the null function, it would look like this instead:
[code=javascript]
createDOM("img" , "divlogin", "loginimg", "Login", "../images/defaultLogin.jp g", "null(8)", "loginForm( )", page.length - 2);
[/code]
Which is much better, but perhaps still not great. Am I on the right track, or am I way off base here? Is there a better way to do this, either in the null function or in the original createDOM() variable list?
Thanks.
Comment