![]() |
[jQuery] MouseDown and Up when toggle-ing
Hi all,
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. Code:
$("ul.checklist label").mousedown(function() {Thanks already. |
Woow, a lot of dust in this thread... :D
(bump) |
Looks like you misunderstand what the toggle method does: http://api.jquery.com/toggle/
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). |
Hey venegal, thanks.
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. |
I was wrong about the toggle — jQuery provides two toggle methods, and I thought of the other one.
Everything Javascript-related seems to be in order. This is a pure CSS issue: You have Code:
.checklist label {Code:
.unchecked {.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. |
Thanks venegal, I totally forgot that .checklist label {} has the upperhand in these css rules. Thanks for pointing this out.
|
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? |
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. |
Thanks a lot, venegal.
Hardcoding the unchecked class seems obvious, now you hinted me that out. :) Sorry for the hassle, and again many thanks. |
| All times are GMT +1. The time now is 07:39 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.