OK, Javascript gurus, why is "if" not evaluating as true or false ?
-
For example:
// Wow, more stupid Javascript. We can't just do this: // return ($(e.target).parents('.jqx-tree').length > 0) // We actually have to do this. WTF? if ($(e.target).parents('.jqx-tree').length > 0) { return false; } return true;
WTF is the reason I can't just do a return on > comparison??? Marc
Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project!
I can't reproduce the problem. Given:
var result = $(" ... ").length > 0;
alert(typeof result);
alert(result);the code displays "boolean" and "true". What output do you get if you try:
var result = ($(e.target).parents('.jqx-tree').length > 0);
alert(typeof result);
alert(result);
return result;As a last resort, there's always the "
!!
" trick to convert to a boolean value:return !!($(e.target).parents('.jqx-tree').length > 0);
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
I can't reproduce the problem. Given:
var result = $(" ... ").length > 0;
alert(typeof result);
alert(result);the code displays "boolean" and "true". What output do you get if you try:
var result = ($(e.target).parents('.jqx-tree').length > 0);
alert(typeof result);
alert(result);
return result;As a last resort, there's always the "
!!
" trick to convert to a boolean value:return !!($(e.target).parents('.jqx-tree').length > 0);
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Yeah, it looked like a boolean to me too!
!!
didn't work, but SO provided what I think is a better solution anyways:function disableBrowserContextMenu() { $(document).on('contextmenu', function (e) { if ($(e.target).parents('.jqx-tree').length > 0) { e.preventDefault() } }); }
Note the
e.preventDefault()
. Somehow, the explicit return true/false must have been doing this behind the scenes, I'm wildly guessing. MarcImperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project!
-
Yeah, it looked like a boolean to me too!
!!
didn't work, but SO provided what I think is a better solution anyways:function disableBrowserContextMenu() { $(document).on('contextmenu', function (e) { if ($(e.target).parents('.jqx-tree').length > 0) { e.preventDefault() } }); }
Note the
e.preventDefault()
. Somehow, the explicit return true/false must have been doing this behind the scenes, I'm wildly guessing. MarcImperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project!
In jQuery event handlers,
return false;
should be directly equivalent toe.preventDefault(); e.stopPropagation();
, and usually works to cancel an event. Having said that,e.preventDefault()
is the preferred way to stop the event. The difference between ‘return false;’ and ‘e.preventDefault();’[^] jQuery Events: Stop (Mis)Using Return False[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
In jQuery event handlers,
return false;
should be directly equivalent toe.preventDefault(); e.stopPropagation();
, and usually works to cancel an event. Having said that,e.preventDefault()
is the preferred way to stop the event. The difference between ‘return false;’ and ‘e.preventDefault();’[^] jQuery Events: Stop (Mis)Using Return False[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Richard Deeming wrote:
In jQuery event handlers,
Heh, this is what happens when a noob (which I still consider myself) starts doing Javascript. Good to know, and thank you for the great info! Marc
Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project!
-
I can't reproduce the problem. Given:
var result = $(" ... ").length > 0;
alert(typeof result);
alert(result);the code displays "boolean" and "true". What output do you get if you try:
var result = ($(e.target).parents('.jqx-tree').length > 0);
alert(typeof result);
alert(result);
return result;As a last resort, there's always the "
!!
" trick to convert to a boolean value:return !!($(e.target).parents('.jqx-tree').length > 0);
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
I didn't know that
!!
operator. Thanks. ;-) Removed irrelevant contentThe shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
-
For example:
// Wow, more stupid Javascript. We can't just do this: // return ($(e.target).parents('.jqx-tree').length > 0) // We actually have to do this. WTF? if ($(e.target).parents('.jqx-tree').length > 0) { return false; } return true;
WTF is the reason I can't just do a return on > comparison??? Marc
Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project!
To take a stab at the original question, jquery functions always return collections which is why a) can chain things b) never have to check for nulls. If $(e.target) returned a single thing and parents('.jqx-tree') returned a single thing then your code would make sense and work. But you actually have a foreach loop acting on a foreach loop...the first $() returns a collection (even if that collection only has one item) and the .parents works on each item in that collection and returns its own collection which your .length acts on. Now imaging $(..) returns 3 items (a, b and c) and the .parents of each of those items returns 1,0 and 5 items. a.parents.length = 1 b.parents.length = 0 c.parents.length = 5 now your code
if ($(e.target).parents('.jqx-tree').length > 0)
makes less sense, which "length" are you evaluating? This idea about returning collections is probably what is stopping your code from working how you would expect. Maybe :) someone might know better.
-
To take a stab at the original question, jquery functions always return collections which is why a) can chain things b) never have to check for nulls. If $(e.target) returned a single thing and parents('.jqx-tree') returned a single thing then your code would make sense and work. But you actually have a foreach loop acting on a foreach loop...the first $() returns a collection (even if that collection only has one item) and the .parents works on each item in that collection and returns its own collection which your .length acts on. Now imaging $(..) returns 3 items (a, b and c) and the .parents of each of those items returns 1,0 and 5 items. a.parents.length = 1 b.parents.length = 0 c.parents.length = 5 now your code
if ($(e.target).parents('.jqx-tree').length > 0)
makes less sense, which "length" are you evaluating? This idea about returning collections is probably what is stopping your code from working how you would expect. Maybe :) someone might know better.
But
e.target
will be a single DOM element, so$(e.target)
will be a jQuery wrapper containing a single element. And even if it contained three elements, the.parents(...)
call would return a jQuery wrapper containing the matching parents of all three elements. So in your example, the.length
property would return 6.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
But
e.target
will be a single DOM element, so$(e.target)
will be a jQuery wrapper containing a single element. And even if it contained three elements, the.parents(...)
call would return a jQuery wrapper containing the matching parents of all three elements. So in your example, the.length
property would return 6.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Richard Deeming wrote:
But
e.target
will be a single DOM element, so$(e.target)
will be a jQuery wrapper containing a single element.It will be a collection that contains a single element. new string[]{"hello"} is not interchangeable with "hello" Like I said, I don't know js well enough to know if this is the reason that the original code isn't working as intended, I'm just saying it might be a possible reason.
-
Richard Deeming wrote:
But
e.target
will be a single DOM element, so$(e.target)
will be a jQuery wrapper containing a single element.It will be a collection that contains a single element. new string[]{"hello"} is not interchangeable with "hello" Like I said, I don't know js well enough to know if this is the reason that the original code isn't working as intended, I'm just saying it might be a possible reason.
All jQuery wrapper objects are collections, whether they contain multiple elements, a single element, or no elements.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
All jQuery wrapper objects are collections, whether they contain multiple elements, a single element, or no elements.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Yes, that's what I'm saying :)