Bug Tracker

Modify

Ticket #7266 (closed enhancement: invalid)

Opened 3 years ago

Last modified 2 years ago

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:
Blocking: Blocked by:

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

comment:1 follow-up: ↓ 2 Changed 3 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: ↓ 3 Changed 3 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 3 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 3 years ago by f.mardini@…

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

comment:5 Changed 3 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 3 years ago by SlexAxton

  • Status changed from new to closed
  • Resolution set to invalid
  • Component changed from unfiled to core
  • 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.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.