Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jquery attribute selector = vs !=

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm puzzled by the output of these. I get:
9
7
0



I thought = and != were opposites as selectors. The fact that != works implies it is in fact checking for the value 2 and spaces or the like aren't messing it up. I've created the third one by copy pasting the working line and removing the exclamation point so I know it isn't a typo. Any ideas what is going on here?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I have a workaround that lets me proceed, but no insight to why. The above returns 2. (9-7). Which is correct. But it seems like it should be exactly the same as the = selector.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65226
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post an SSCCE that exhibits the behavior?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. I didn't include one before because I thought it would be obvious. After all, I'm sure the = selector works as it should.

The following example displays:
2 - expected
1 - expected
0 - the problem
1- expected

I'm on Mac Lion/Safari. Same behavior on Mac Lion/Chrome. This is from a proof of concepts for robotics (not work) so I didn't spend the time getting every shred of "business" meaning out of it.

 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe this will make you understand what is happening

http://jsfiddle.net/2DFA2/ [Needs console open]

Compare the initial load vs the button click with the logs. Notice anything?

Eric
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I notice that != looks like it uses the udpated values and = does not. And that jfiddle is cool.

This doesn't help though as I still don't see why. Or even if that is the problem.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The value property does not get updated when the value changes, hence why the selector fails. I think there is a jQuery bug logged for this. You need to use filter()

http://jsfiddle.net/2DFA2/1/

Eric
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ugh. Ok thanks. It's odd that it works for !=.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65226
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric Pascarello wrote:The value property does not get updated when the value changes ...

Actually, the value property does update when the control value is changed; it's the value attribute that doesn't change, and that, of course, is what the CSS selector is checking against.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic