
Originally Posted by
cbiti
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:
Code:
isUndefinedOrNull = function(o) {
return typeof(o) == 'undefined' || o === 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:
Code:
if (isUndefinedOrNull($("foo"))) {
alert("no foo");
} else {
alert("there's a foo, alright");
}
Bookmarks