Tuesday, June 4, 2024
 Popular · Latest · Hot · Upcoming
188
rated 0 times [  195] [ 7]  / answers: 1 / hits: 49275  / 13 Years ago, fri, april 1, 2011, 12:00:00

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.


More From » javascript

 Answers
103

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) ...

[#92954] Thursday, March 31, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dorab

Total Points: 22
Total Questions: 106
Total Answers: 99

Location: El Salvador
Member since Fri, May 8, 2020
4 Years ago
;