I have working toggle labels for adding/removing table rows.
These labels have a sprite checkbox background, which holds 4 different states:
- unchecked
- unchecked-highlight
- checked
- checked-highlight
These states need to appear when pressing and releasing a label, so mousedown and mouseup.
However, with the code below, there is no background change at all.
I've used classes for the different background-positions.
Also, since this depends on your HTML and CSS to work, it would be easier if you showed a live example, once you've replaced that toggle thing with something that works. I suppose all you need is a mousedown and a click handler, with the mousedown going from (un)checked to (un)checked-highlight, and the click going from checked-highlight to unchecked (and from unchecked-highlight to checked).
__________________
.My new Javascript tutorial site: http://reallifejs.com/
.Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
.Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback
Here's a live demo. http://gigifrituur.be/checklist_demo/
The html and css should be fine, and toggle works, it's just the mousedown and mouseup that doesn't work.
in there. You were apparently hoping that the .unchecked styles would overwrite the .checklist label styles, which they won't, because the .checklist label selector is more specific than the .unchecked selector.
So, either make those other selectors more specific, like .checklist label.unchecked, or add an !important to their styles.
__________________
.My new Javascript tutorial site: http://reallifejs.com/
.Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
.Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback
Hmm, one more thing though, venegal, if you don't mind.
The first time when toggle is used, there is no unchecked-highlight background. This is the only issue, because after the first time, toggle registers everything inside.
Is there an alternative for toggle, so it will also register the first "click", instead of only "activating" it?
Why is toggle made this way? What's the purpose for not handling it the first time?
Last edited by fishbaitfood; 11-14-2011 at 06:46 PM..
This has nothing to do with the toggle method — first of all, the toggle method internally uses a click event, which won't fire before mouseup, so naturally it won't do anything on mousedown (which is when the highlighting is supposed to happen). And secondly, the functions you're passing to the toggle method don't have anything to do with the "highlight" classes anyway.
The problem is your mousedown handler: It will only add the class "unchecked-highlight", if the element already has the class "unchecked". In the very beginning, those elements simply don't have that class. So, either add those "unchecked" classes to your HTML, or add them programmatically before any toggling action takes place.
__________________
.My new Javascript tutorial site: http://reallifejs.com/
.Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
.Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback