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.

4

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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) ...
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'.
I'll have to try with typeof first. Simply doing if(selection) is still showing an empty alert box.
@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: jsfiddle.net/2mm5n
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) ...

2 Comments

@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.
@Mike: Intrigued by the lack of quality of the functions that can be found, I put together an alternative that returns a consistent result: blog.guffa.com/2011/04/get-selected-text-a-better-way
3

you can simply use:

if (selection) {
  alert(selection);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.