#7266 closed enhancement (invalid)
jQuery show and hide functions don't work properly when returned by ternary statements
Reported by: | Owned by: | ||
---|---|---|---|
Priority: | undecided | Milestone: | 1.5 |
Component: | core | Version: | 1.4.3 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
In the test case I link to in this bug, I use a ternary statement to decide whether to run hide or show. However, if the condition for the ternary is a statement, the functions are not returned properly. This is demonstrated by using ternary statements to hide both 'Div X', 'Div Y' and 'Div Z'. However, although all the written ternary statements are supposed to be equivalent, the second one does not work.
This was tested on both Firefox and Chrome on mac.
Change History (6)
comment:1 follow-up: 2 Changed 12 years ago by
comment:2 follow-up: 3 Changed 12 years ago by
The problem is that it works correctly when the condition is true, but when the condition is an expression, it doesn't work. Shouldn't the behavior be the same regardless of the type of the condition used?
Replying to caii:
not jquery bug , see:
m={a:"cmc",f:function(){alert(this.a)}}
m.f();/*cmc*/
n=function(){};
(1==1?m.f:n)()/*undefined*/
you lost the 'this' in show/hide function
comment:3 Changed 12 years ago by
Replying to anonymous:
The problem is that it works correctly when the condition is true, but when the condition is an expression, it doesn't work. Shouldn't the behavior be the same regardless of the type of the condition used?
Yes, when the condition is true it works in Firefox. Maybe it's Firefox's bug..
(true ? $("#x").hide : $("#x").show)(); Equivalent to ($("#x").hide)() in firefox so it works.
when the condition is an expression, the object return by the "?:" is a function as it under window,so the function can't executive for the 'this' in the function body are window.
sorry for my poor english :)
comment:5 Changed 12 years ago by
Did you mean to link to http://jsfiddle.net/YMs5b/3/ ?
In Opera only DIV Z
is hidden which makes sense as explained by caii
comment:6 Changed 12 years ago by
Component: | unfiled → core |
---|---|
Resolution: | → invalid |
Status: | new → closed |
Type: | bug → enhancement |
This is not a bug in jQuery, just a misunderstanding of how the ternary operator and jQuery chainability works.
not jquery bug , see:
m={a:"cmc",f:function(){alert(this.a)}}
m.f();/*cmc*/
n=function(){};
(1==1?m.f:n)()/*undefined*/
you lost the 'this' in show/hide function