The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

passing object name as parameter

Greetings,I'm trying to write a generic isNullEmpty function that takes an object name as a parameter and checks if it exisits and/or if its value is empty. how can I check the existance of an object name when it's passed in as a function parameter? the code below does NOT work. Thanks.

kyberfabrikken, visual basic has a isNullEmpty property for visual basic strings. my thought is to write this function where I can pass in a string, which can be an object name, or even an element ID and I can check whether that elment exisits document.getElementByID('passedinID') and get it's value to see if it's empty. also I want to use this function to passin global object names and simply check if they exist and/or if their values are empty. I hope this gives you an idea about what I'm trying to do here. Thanks.

Pepejeria, I guess I see your point in this. but going back to my original question, for a future reference, I would like to know how to execute variable names/object names when they're passed in as a parameter. Thanks.

kyberfabrikken, visual basic has a isNullEmpty property for visual basic strings. my thought is to write this function where I can pass in a string, which can be an object name, or even an element ID and I can check whether that elment exisits document.getElementByID('passedinID') and get it's value to see if it's empty. also I want to use this function to passin global object names and simply check if they exist and/or if their values are empty. I hope this gives you an idea about what I'm trying to do here. Thanks.

Not knowing vb, I would guess that isNullEmpty() checks if the value of a variable is null or the variable is undefined. You can implement this by checking if the typeof operator returns "undefined" or the value equals null:

Combining the function with a check for the existence of an element in the document, would be a bad idea IMHO. Variables in javascript and elements in the DOM are two different things, even if they are tightly integrated. You can create a wrapper function over the horridly longish document.getElementById(), to save the typing though. Most people use $ as the name for such a function - an idea initially coming from the prototype-framework. An implementation could simply be:

Code:

$ = function(id) {
return document.getElementById(id);
}

You can then combine the two functions in one expression, to check if a given element exists in your document: