468,301 Members | 1,499 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,301 developers. It's quick & easy.

Autofill Multiple "null" values in function variable list

RMWChaos
137 100+
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:

Expand|Select|Wrap|Line Numbers
  1. // any one of these could be "null" depending on the DOM element being created
  2.  
  3. function createDOM(dom, parent, id, alt, src, method, type, value, size, name, text, onlcick, pSelect)
  4.  
  5. {
  6.  
  7. var d;
  8.  
  9. var null(d);
  10.  
  11. var newNull = "null";
  12.  
  13. for (null(d); d > 0; --d)
  14.  
  15.   {
  16.  
  17.   newNull += ",null";
  18.  
  19.   };
  20.  
  21. null(d) = newNull;
  22.  
  23. (...)
  24.  
  25. }
  26.  
So without the null function, an example createDOM might look like this:

Expand|Select|Wrap|Line Numbers
  1. createDOM("img", "divlogin", "loginimg", "Login", "../images/defaultLogin.jpg", null, null, null, null, null, null, null, null, "loginForm()", page.length - 2);
  2.  
A real pain in the arse, eh? But with the null function, it would look like this instead:

Expand|Select|Wrap|Line Numbers
  1. createDOM("img", "divlogin", "loginimg", "Login", "../images/defaultLogin.jpg", "null(8)", "loginForm()", page.length - 2);
  2.  
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.
Oct 26 '07 #1
5 1964
gits
5,390 Expert Mod 4TB
hi ...

its ugly ;) ... i think you didn't ask in case you didn't find that too? :)

you may use optional params ... the following way:

Expand|Select|Wrap|Line Numbers
  1. /**
  2.  * @params params object with properties
  3.  */
  4. function test(params) {
  5.     // now you may do (set a default value in case you don't pass it):
  6.     if (typeof params.prop1 == 'undefined') {
  7.         params.prop1 = 'dflt_value';
  8.     }
  9.  
  10.     // later on you use params.prop1
  11. }
  12.  
  13. // usage
  14. test({ prop2: 1, prop3: 2 });
  15.  
kind regards
Oct 26 '07 #2
mrhoo
428 256MB
or, you could always pass the constructor a tag name and an attributes object,
and never miss your null.

Expand|Select|Wrap|Line Numbers
  1. document.createDom= function(tag, attr){
  2.     var el= document.createElement(tag);
  3.     for(var p in attr) el[p]= attr[p];
  4.     return el;
  5. }
var el=document.createDom('img',{src:'imgsrc.gif',widt h:'100px',alt:'sometext'});
Oct 27 '07 #3
RMWChaos
137 100+
hi ...

its ugly ;) ... i think you didn't ask in case you didn't find that too? :)

you may use optional params ... the following way:
Oh yeah, totally ugly! Which is why I have chosen to use optional params in a JSON property list. By the way, I did find a way to do the null counter with this code:

Expand|Select|Wrap|Line Numbers
  1. function nullcount(d) // Auto-fill null values
  2.  
  3.     {
  4.  
  5.     var newNull = null;
  6.  
  7.     while (--d > 0)
  8.  
  9.         {
  10.  
  11.         newNull += " ,null";
  12.  
  13.         };
  14.  
  15.     return newNull;
  16.  
  17.     };
  18.  
But I hadn't quite figured out how to remove the quotes around the data that returns, "null, null, null..." instead of null, null, null...so my functions saw it as a single parameter instead of individual ones.

Anyway, I like the JSON way better because I don't have to worry about the order, null placeholders, or even including all the variables. If I can just get around the excessive recursion error, my code will be golden.

I'll probably end up posting a new discussion with my new DOMLoader code tonight just to get some other eyes on it looking for ways to improve things, especially if I can't figure out this recursion issue.

Thanks for your help!
Oct 27 '07 #4
RMWChaos
137 100+
or, you could always pass the constructor a tag name and an attributes object, and never miss your null.
Hm, maybe it's because I've been looking at JSON property lists for the last 8 hours, but I am having trouble following your code. I need to break it out and play with it a while to understand what you are saying.

But thank you very much for your reply! You have given me something to learn, which is the best gift anyone can give me! :-)))
Oct 27 '07 #5
gits
5,390 Expert Mod 4TB
or, you could always pass the constructor a tag name and an attributes object,
and never miss your null.

Expand|Select|Wrap|Line Numbers
  1. document.createDom= function(tag, attr){
  2.     var el= document.createElement(tag);
  3.     for(var p in attr) el[p]= attr[p];
  4.     return el;
  5. }
var el=document.createDom('img',{src:'imgsrc.gif',widt h:'100px',alt:'sometext'});
yep ;) ... that's it ... slick, short ... the way you should do it ... and combine it with the undefined check i showed you in case you need other attributes set to any default ...

kind regards
Oct 27 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

26 posts views Thread by Agoston Bejo | last post: by
3 posts views Thread by Karunakararao | last post: by
8 posts views Thread by craigkenisston | last post: by
2 posts views Thread by Stanislav Simicek | last post: by
2 posts views Thread by Anastasios Papadopoulos | last post: by
6 posts views Thread by doncee | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.