Bug Tracker

Opened 6 years ago

Closed 6 years ago

Last modified 4 years ago

#7266 closed enhancement (invalid)

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

Reported by: f.mardini@… 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/

Change History (6)

comment:1 follow-up: Changed 6 years ago by 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:2 in reply to: ↑ 1 ; follow-up: Changed 6 years ago by 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?

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 in reply to: ↑ 2 Changed 6 years ago by caii

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:4 Changed 6 years ago by f.mardini@…

It makes sense now, thanks It's definitely a FF issue

comment:5 Changed 6 years ago by jitter

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 6 years ago by SlexAxton

  • Component changed from unfiled to core
  • Resolution set to invalid
  • Status changed from new to closed
  • Type changed from bug to enhancement

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

Note: See TracTickets for help on using tickets.