Skip to main content

Bug Tracker

Side navigation

#7266 closed enhancement (invalid)

Opened October 21, 2010 12:10PM UTC

Closed October 21, 2010 05:33PM UTC

Last modified March 14, 2012 01:08AM UTC

jQuery show and hide functions don't work properly when returned by ternary statements

Reported by: f.mardini@gmail.com 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.

http://jsfiddle.net/YMs5b/

Attachments (0)
Change History (6)

Changed October 21, 2010 01:02PM UTC by caii comment:1

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

Changed October 21, 2010 01:07PM UTC by anonymous comment:2

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 [comment:1 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

Changed October 21, 2010 01:19PM UTC by caii comment:3

Replying to [comment:2 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 :)

Changed October 21, 2010 03:20PM UTC by f.mardini@gmail.com comment:4

It makes sense now, thanks

It's definitely a FF issue

Changed October 21, 2010 04:16PM UTC by jitter comment:5

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

Changed October 21, 2010 05:33PM UTC by SlexAxton comment:6

component: unfiledcore
resolution: → invalid
status: newclosed
type: bugenhancement

This is not a bug in jQuery, just a misunderstanding of how the ternary operator and jQuery chainability works.