views:

252

answers:

3

Hi!

What is the best way to test for an empty string with jquery-out-of-the-box?

I.e., without plugins. I tried this:

http://zipalong.com/blog/?p=287

But it did't work at least out-of-the-box. It would be nice to use something that's builtin.

I wouldn't like to repeat

"if (a == null || a=='')"

everywhere if some "if (isempty(a))" would be available.

A: 
if(!my_string){ 
// stuff 
}

and

if(my_string !== "")

if you want to accept null but reject empty

EDIT: woops, forgot your condition is if it IS empty

Greg
+5  A: 
if (!a) {
  // is emtpy
}
David
If this is true, then thanx!
Martin
+2  A: 

The link you gave seems to be attempting something different to the test you are trying to avoid repeating.

if (a == null || a=='')

tests if the string is an empty string or null. The article you linked to tests if the string consists entirely of whitespace (or is empty).

The test you described can be replaced by:

if (!a)

Because in javascript, an empty string, and null, both evaluate to false in a boolean context.

SpoonMeiser