9

Not having much luck with this. I'm trying to determine if a var is not empty.

$('#content').mouseup(function() {

    var selection = getSelected();

    if (typeof(selection) !=='undefined') {
        alert(selection);
    }
});

What this is doing is grabbing any text the user has selected -- but it shows an empty alert even if the user just mouseup's on the div.

Mike
  • 980
  • 3
  • 15
  • 29

3 Answers3

15

Just say:

if (selection) {
    alert(selection);
}

The simple true/false test in Javascript returns true if the member is defined, non-null, non-false, non-empty string, or non-zero.

Also, in Javascript, something can be equal to a value undefined or actually undefined (meaning, no such named object exists). e.g.

var x = undefined;
alert(x===undefined); /* true; */
alert(x); /* false */
x=1;
alert(x); /* true */

alert(y===undefined); /* reference error - there's nothing called y */
alert(y); /* reference error */
alert(typeof y === "undefined"); /* true */

As the comment below notes, if you are not sure if something even exists at all, you should test that first using typeof.

Jamie Treworgy
  • 23,934
  • 8
  • 76
  • 119
  • 1
    It should be noted that this will fail if the variable has not been declared; it will raise a `ReferenceError`. If you need to make sure the variable exists *and* it has some truthy value: `if (typeof selection !== 'undefined' && selection) ...` – Reid Apr 01 '11 at 23:15
  • True 'nuf - in OP's Q he explicitly defines 'selection', so I simplified, but your note is without question an important part of this foundation of knowledge. Actually I think my answer might be slightly confusing when I say "is defined." The reality is confusing since something can be actually undefined, or equal to 'undefined'. – Jamie Treworgy Apr 01 '11 at 23:18
  • I'll have to try with typeof first. Simply doing if(selection) is still showing an empty alert box. – Mike Apr 02 '11 at 00:24
  • 1
    @Mike, if it was not actually defined, then `alert(selection)` would cause an error. If you are getting an empty alert box, it's because the value of `selection` is something that's blank but not an empty string. Maybe it's a single space or something. I have no idea what that function returns, but it's not returning an empty string, or the selection wouldn't show. See: http://jsfiddle.net/2mm5n/ – Jamie Treworgy Apr 02 '11 at 12:41
8

Your code is perfectly accurate for detecting an undefined value, which means that the function always returns some kind of value even if there is no selection.

If the function for example returns a selection object (like the window.getSelection function), you check the isCollapsed property to see if the selection is empty:

if (!selection.isCollapsed) ...
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • @Mike: I am afraid that it will only work that way in some browers. The function that you are using returns different things depending on the browser, either an object, a string or a boolean. The function should be rewritten to return a consistent result, or it will be very hard to use. – Guffa Apr 02 '11 at 00:59
  • @Mike: Intrigued by the lack of quality of the functions that can be found, I put together an alternative that returns a consistent result: http://blog.guffa.com/2011/04/get-selected-text-a-better-way/ – Guffa Apr 02 '11 at 12:36
3

you can simply use:

if (selection) {
  alert(selection);
}
danniel
  • 1,751
  • 1
  • 11
  • 13