Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-10-2011, 11:25 AM   PM User | #1
fishbaitfood
New Coder

 
Join Date: Nov 2011
Posts: 30
Thanks: 5
Thanked 0 Times in 0 Posts
fishbaitfood is an unknown quantity at this point
[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() {
	if ($(this).hasClass("checked")) {
		$(this).removeClass("checked").addClass("checked-highlight");
	}
	else if ($(this).hasClass("unchecked")) {
		$(this).removeClass("unchecked").addClass("unchecked-highlight");
	}
});

$("ul.checklist label").mouseup(function() {
	if ($(this).hasClass("checked-highlight")) {
		$(this).removeClass("checked-highlight").addClass("checked");
	}
	else if ($(this).hasClass("unchecked-highlight")) {
		$(this).removeClass("unchecked-highlight").addClass("unchecked");
	}
});

$("ul.checklist label").toggle(
	function() {
		$(this).removeClass("unchecked").addClass("checked");
		$(this).prev().attr("checked", true);
	}, function() {
		$(this).removeClass("checked").addClass("unchecked");
		$(this).prev().attr("checked", false);
	}
);
How do I make the mousedown and mouseup functions work?

Thanks already.
fishbaitfood is offline   Reply With Quote
Old 11-13-2011, 12:30 PM   PM User | #2
fishbaitfood
New Coder

 
Join Date: Nov 2011
Posts: 30
Thanks: 5
Thanked 0 Times in 0 Posts
fishbaitfood is an unknown quantity at this point
Woow, a lot of dust in this thread...

(bump)
fishbaitfood is offline   Reply With Quote
Old 11-13-2011, 03:14 PM   PM User | #3
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
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).
__________________
.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
venegal is offline   Reply With Quote
Old 11-14-2011, 03:22 PM   PM User | #4
fishbaitfood
New Coder

 
Join Date: Nov 2011
Posts: 30
Thanks: 5
Thanked 0 Times in 0 Posts
fishbaitfood is an unknown quantity at this point
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.
fishbaitfood is offline   Reply With Quote
Old 11-14-2011, 04:00 PM   PM User | #5
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
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 {
	background: url('images/checkbox.png') 0 0 no-repeat;
}
and stuff like

Code:
.unchecked {
	background-position: 0 0;
}
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
venegal is offline   Reply With Quote
Users who have thanked venegal for this post:
fishbaitfood (11-14-2011)
Old 11-14-2011, 05:08 PM   PM User | #6
fishbaitfood
New Coder

 
Join Date: Nov 2011
Posts: 30
Thanks: 5
Thanked 0 Times in 0 Posts
fishbaitfood is an unknown quantity at this point
Thanks venegal, I totally forgot that .checklist label {} has the upperhand in these css rules. Thanks for pointing this out.
fishbaitfood is offline   Reply With Quote
Old 11-14-2011, 06:43 PM   PM User | #7
fishbaitfood
New Coder

 
Join Date: Nov 2011
Posts: 30
Thanks: 5
Thanked 0 Times in 0 Posts
fishbaitfood is an unknown quantity at this point
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..
fishbaitfood is offline   Reply With Quote
Old 11-14-2011, 07:00 PM   PM User | #8
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
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
venegal is offline   Reply With Quote
Users who have thanked venegal for this post:
fishbaitfood (11-14-2011)
Old 11-14-2011, 07:28 PM   PM User | #9
fishbaitfood
New Coder

 
Join Date: Nov 2011
Posts: 30
Thanks: 5
Thanked 0 Times in 0 Posts
fishbaitfood is an unknown quantity at this point
Thanks a lot, venegal.
Hardcoding the unchecked class seems obvious, now you hinted me that out.

Sorry for the hassle, and again many thanks.
fishbaitfood is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:23 PM.


Advertisement
Log in to turn off these ads.