views:

86

answers:

6

When I want to check if an element exists in a page. Are these 2 checks the same? Is there a better more compact way to check the existence. What if I want to check if the value==''. Can this be included in this check as well somehow.

+3  A: 
Pointy
There is also a lot of good info in this post. (But I prefer to always check typeof by habit.)
Andir
Checking "typeof" in this case is completely useless because the return value from `getElementById` will always be type "object", even when it's null.
Pointy
For me, the whole point of jquery is that you would not need the "if". Just the jquery selector.
NimChimpsky
@batthink agreed - it's pretty rare that you need to check like that, but not outlandish.
Pointy
A: 

document.getElementById('something') can be 'undefined'. Usually (thought not always) it's sufficient to do tests like if (document.getElementById('something')).

Robusto
you forgot closing bracket
Andreas Niedermair
@Andreas Niedermair: Thanks. Edited to add that.
Robusto
A: 

Yeah. Try this.. lazy evaluation should prohibit the second part of the condition from evaluating when the first part is false/null:

var someval = document.getElementById('something')
if (someval && someval.value <> '') {
Fosco
I would not recommend needlessly calling `getElementById` twice like that.
Pointy
No need to evaluate `document.getElementById('something')` twice.
Gumbo
He specifically asked to check that the value of the element not equal '', at least that is how I read his intent..
Fosco
Yeah, in which case you store a the element you get back from the getElementById() call and use that instead of calling getElementById() twice. See my answer.
Andir
Ok, valid point. Thanks.
Fosco
A: 

jquery will provide you with this and more ...

if($("#something").val()){ //do stuff}

It took me a couple of days to pick it up, but it provides you with you with so much more functionality. An example below.

jQuery(document).ready(function() {
    /* finds closest element with class divright/left and 
    makes all checkboxs inside that div class the same as selectAll...
    */
        $("#selectAll").click(function() {
        $(this).closest('.divright').find(':checkbox').attr('checked', this.checked);
    });
});
NimChimpsky
"Use JQuery" is worth an upvote all on its own? Not even "If you're using JQuery, you could do it this way..."? Man... Maybe I'd have more rep if I just posted "JQuery can do it" in every JavaScript thread.
Tim Goodman
lol. Good advice imho : use jquery for every javascript problem that is.
NimChimpsky
jQuery is "good advice" but it's not what was being asked. I love what jQuery provides and use it extensively. It's a great framework to get the hang of.
Andir
+2  A: 

It's better (but wordier) to use:

var element = document.getElementById('something');
if (typeof element !== "undefined" && element.value == '') {
}

Edit: Added value == '' condition.

Edit2: I was corrected. getElementById() will indeed return an object, null or not. So a != null check will work where the above will not. Yet another reason to check your work. ;) Thanks.

var element = document.getElementById('something');
if (element != null && element.value == '') {
}
Andir
Why is that better?
Tim Goodman
There are many ways to check for definition. And many ways to break them. Say for instance you type (if (someValue) {}): What if someValue == false? It's defined, but it's false so the condition fails. If you use (if (someValue != undefined) {}): I can break your code by setting undefined = true. typeof always returns a type string and if the object you are testing is not defined, it returns "undefined").
Andir
In this *specific* case, he's calling `getElementById()`. We *know* what that will return. It cannot return the constant `false`, for example. It will return an element, or `null`.
Pointy
Also, this is just plain wrong anyway because "typeof null" is *not* "undefined"!!
Pointy
Wow, setting undefined = true ... yikes. I'd sure like to know what the rationale was for that being allowed. Anyway, good explanation, thanks.
Tim Goodman
@Pointy: Oh, you are right... it returns a null object... hmm, edit time.
Andir
@Tim you can use `(void 0)` instead of `undefined` if you're paranoid.
Pointy
Cool, thanks for the tip, @Pointy.
Tim Goodman
A: 

I use this,vide: What is the best way to check for an empty string in JavaScript?

Jet