Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Special Interests > Visual Media > Web Design and Development

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 3, 2011, 03:47 PM   #1
Hexiii
macrumors 6502a
 
Join Date: Jun 2011
Location: Prague, Czech Republic
jQuery div toggle

So, I have about 7 divs and every div is with different class. I want them to toggle SEPERATELY when I hover across it.

This is the function:
Code:
$(".open").hide();

	$("div.click").mouseenter(function(){
		$(".open").toggleClass("active").animate({width:'toggle'},1000);
	});
But if I want to have 7 div classes which every class toggle seperately, do I have to copy it 7 times and just change the class in every script? Because if I put a comma between each class it will toggle them at one time. So, any ideas?
Hexiii is offline   0 Reply With Quote
Old Nov 3, 2011, 04:17 PM   #2
jsm4182
macrumors 6502
 
Join Date: Apr 2006
Location: Newburgh, NY
Send a message via AIM to jsm4182
Give all the divs the same class.

To have the function only applied to the one thats hovered over change $(".open") to $(".open", this).

Adding this changes the scope to only the element that triggered the function.
__________________
MacBook Pro 13", 2.53ghz C2D, 8GB Ram, 640GB HDD, ML | iPhone 5 32GB | iPad 3 64GB
jsm4182 is offline   0 Reply With Quote
Old Nov 3, 2011, 04:23 PM   #3
Hexiii
Thread Starter
macrumors 6502a
 
Join Date: Jun 2011
Location: Prague, Czech Republic
Thanks, I'll try that.
Hexiii is offline   0 Reply With Quote
Old Nov 4, 2011, 10:05 AM   #4
Hexiii
Thread Starter
macrumors 6502a
 
Join Date: Jun 2011
Location: Prague, Czech Republic
Well, it doesn't work
Hexiii is offline   0 Reply With Quote
Old Nov 4, 2011, 10:38 AM   #5
jared_kipe
macrumors 68030
 
jared_kipe's Avatar
 
Join Date: Dec 2003
Location: Seattle
Send a message via AIM to jared_kipe
I think he meant jQuery(this)

EDIT: Actually I'm not sure what you are intending to do. Are you basically trying to remove the class 'active' from all but the one hovered? I assume all of them start with out any having the class 'active', so on the hover of one just jQuery('.open').removeClass('active'); jQuery(this).addClass('active').animatewhatever...

What isn't working about it?

Last edited by jared_kipe; Nov 4, 2011 at 10:45 AM.
jared_kipe is offline   0 Reply With Quote
Old Nov 4, 2011, 10:42 AM   #6
Hexiii
Thread Starter
macrumors 6502a
 
Join Date: Jun 2011
Location: Prague, Czech Republic
(Well I removed the class active, I don't know why did i put it there.)

I have 7 divs with the same class.
I want them to toggle on mouseenter.
If I hover across one, it will toggle all of them (with the same class)
So what do I have to add to toggle seperately?

Code:
$(".open").hide();

	$("div.click").mouseenter(function(){
		$(".open").animate({width:'toggle'},1000);
	});

Last edited by Hexiii; Nov 4, 2011 at 10:56 AM.
Hexiii is offline   0 Reply With Quote
Old Nov 4, 2011, 12:03 PM   #7
jared_kipe
macrumors 68030
 
jared_kipe's Avatar
 
Join Date: Dec 2003
Location: Seattle
Send a message via AIM to jared_kipe
So the problem is how do you map some div.click's to the .open's. I don't have enough information to solve this problem.

You need to post code, because I can't tell if there is a parent-child relationship or if they are just completely separate.
Code:
<div class="open"><!-- 1 --><div class="click"><!-- 1 --></div></div>
<div class="open"><!-- 2 --><div class="click"><!-- 2 --></div></div>
<div class="open"><!-- 3 --><div class="click"><!-- 3 --></div></div>
Code:
<div class="click"><!-- 1 --></div>
<div class="click"><!-- 2 --></div>
<div class="click"><!-- 3 --></div>

<div class="open"><!-- 1 --></div>
<div class="open"><!-- 2 --></div>
<div class="open"><!-- 3 --></div>
Given the .hide(), I suspect the later. So you will need a way to map them between each other. I usually do this with either hidden text, or an empty link with href="n" so that you can find the n'th .open or something.
jared_kipe is offline   0 Reply With Quote
Old Nov 4, 2011, 12:13 PM   #8
Hexiii
Thread Starter
macrumors 6502a
 
Join Date: Jun 2011
Location: Prague, Czech Republic
Code:
<div id="center">
 
	<div class="click"><a href="#"></a></div>
	<div class="open"><br>
			<h3>XXXX</h3>
			<p><nobr>XXXXX XXXX<nobr></p>
	</div>


	<div class="click left"><a href="#"></a></div>
	
	<div class="open left"><br>
			<h3>XXXX</h3>
			<p><nobr>XXXXX XXXX<nobr></p>
	</div>

</div>
Hexiii is offline   0 Reply With Quote
Old Nov 4, 2011, 12:27 PM   #9
jared_kipe
macrumors 68030
 
jared_kipe's Avatar
 
Join Date: Dec 2003
Location: Seattle
Send a message via AIM to jared_kipe
Easiest modification.
Code:
<div id="center">
 
	<div class="click"><a href="open1"></a></div>
	<div class="open open1"><br>
			<h3>XXXX</h3>
			<p><pre>XXXXX XXXX</pre></p>
	</div>


	<div class="click left"><a href="open2"></a></div>
	
	<div class="open open2 left"><br>
			<h3>XXXX</h3>
			<p><pre>XXXXX XXXX</pre></p>
	</div>

</div>
Code:
jQuery("div.click").mouseenter(function(){
	var classToOpen = jQuery(this).children('a').attr('href');
	jQuery(classToOpen).animate({width:'toggle'},1000);
});
Note that this won't 'un-toggle' the previously toggled ones...
jared_kipe is offline   0 Reply With Quote
Old Nov 4, 2011, 12:29 PM   #10
Hexiii
Thread Starter
macrumors 6502a
 
Join Date: Jun 2011
Location: Prague, Czech Republic
Quote:
Originally Posted by jared_kipe View Post
Easiest modification.

Note that this won't 'un-toggle' the previously toggled ones...
Well, the mine won't either, but thanks, I will try that.

/e It doesn't work either.

Last edited by Hexiii; Nov 4, 2011 at 12:34 PM.
Hexiii is offline   0 Reply With Quote
Old Nov 4, 2011, 12:40 PM   #11
jared_kipe
macrumors 68030
 
jared_kipe's Avatar
 
Join Date: Dec 2003
Location: Seattle
Send a message via AIM to jared_kipe
Quote:
Originally Posted by Hexiii View Post
Well, the mine won't either, but thanks, I will try that.

/e It doesn't work either.
Umm it should. Did you note the changes to the <a href="#" -> <a href="openN", and the addition of the class openN to the div's that you are opening?

EDIT: All this was on the proposition that your previous code WAS working except that it was 'toggling' all of the elements instead of 'toggling' one of them.

EDIT2: NVM napkin programming, I thought of the problem.

the JS should be like this, I didn't make a complete selector out of the href's.
Code:
jQuery("div.click").mouseenter(function(){
	var classToOpen = jQuery(this).children('a').attr('href');
	jQuery('.' + classToOpen).animate({width:'toggle'},1000);
});
You could of course roll that into the a's themselves.

Last edited by jared_kipe; Nov 4, 2011 at 01:04 PM.
jared_kipe is offline   0 Reply With Quote
Old Nov 4, 2011, 01:14 PM   #12
Hexiii
Thread Starter
macrumors 6502a
 
Join Date: Jun 2011
Location: Prague, Czech Republic
Why does this work and mine not?
Hexiii is offline   0 Reply With Quote
Old Nov 4, 2011, 01:34 PM   #13
jared_kipe
macrumors 68030
 
jared_kipe's Avatar
 
Join Date: Dec 2003
Location: Seattle
Send a message via AIM to jared_kipe
because he uses jQuery(this).next() to find the element that is hidden right after the element that he is using to toggle them. This would work for your html. But not if you reordered it.
jared_kipe is offline   0 Reply With Quote
Old Nov 4, 2011, 01:38 PM   #14
Hexiii
Thread Starter
macrumors 6502a
 
Join Date: Jun 2011
Location: Prague, Czech Republic
Quote:
Originally Posted by jared_kipe View Post
because he uses jQuery(this).next() to find the element that is hidden right after the element that he is using to toggle them. This would work for your html. But not if you reordered it.
Yeah, I tried that too, but it didn't work, maybe because one is on the left and one on the right.
Hexiii is offline   0 Reply With Quote
Old Nov 4, 2011, 01:43 PM   #15
jared_kipe
macrumors 68030
 
jared_kipe's Avatar
 
Join Date: Dec 2003
Location: Seattle
Send a message via AIM to jared_kipe
Well obviously the effect depends on the CSS for the various elements, mainly the container element .open
jared_kipe is offline   0 Reply With Quote
Old Nov 4, 2011, 02:06 PM   #16
Hexiii
Thread Starter
macrumors 6502a
 
Join Date: Jun 2011
Location: Prague, Czech Republic
So there isn't any "or" selector in jQuery? something like ("open1"/"open2")
Hexiii is offline   0 Reply With Quote
Old Nov 4, 2011, 02:19 PM   #17
jared_kipe
macrumors 68030
 
jared_kipe's Avatar
 
Join Date: Dec 2003
Location: Seattle
Send a message via AIM to jared_kipe
What would that acomplish? Would it select both of them?
jared_kipe is offline   0 Reply With Quote
Old Nov 4, 2011, 03:12 PM   #18
Hexiii
Thread Starter
macrumors 6502a
 
Join Date: Jun 2011
Location: Prague, Czech Republic
i'd create a separate class for each div and then just put all of them to the jquery
Hexiii is offline   0 Reply With Quote
Old Nov 5, 2011, 08:15 AM   #19
Hexiii
Thread Starter
macrumors 6502a
 
Join Date: Jun 2011
Location: Prague, Czech Republic
Awwww, I found that mistake. I put there <nobr>xxx<nobr> instead of <nobr> xxx </nobr>.

I feel stupid. -.-'
Hexiii is offline   0 Reply With Quote
Old Nov 5, 2011, 09:29 AM   #20
jared_kipe
macrumors 68030
 
jared_kipe's Avatar
 
Join Date: Dec 2003
Location: Seattle
Send a message via AIM to jared_kipe
Quote:
Originally Posted by Hexiii View Post
Awwww, I found that mistake. I put there <nobr>xxx<nobr> instead of <nobr> xxx </nobr>.

I feel stupid. -.-'
Yeah, and I replaced that deprecated tag with <pre></pre> so no excuses
jared_kipe is offline   0 Reply With Quote

Reply
MacRumors Forums > Special Interests > Visual Media > Web Design and Development

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

Similar Threads
thread Thread Starter Forum Replies Last Post
jQuery Selector Question turbobass Web Design and Development 5 Aug 7, 2011 09:10 PM
How can I save div as image where div contains one or more than one HTML5 Canvas? Somnath Web Design and Development 4 Oct 8, 2010 09:04 AM
Expand Parent Div with Size of Child Div krayziekray Web Design and Development 3 Feb 10, 2010 12:24 PM
Centering divs placed in other div :) chameleon81 Web Design and Development 7 Feb 3, 2010 05:41 PM
jQuery, toggle button disable on checkbox change seventeen Web Design and Development 8 Jul 8, 2009 08:56 AM


All times are GMT -5. The time now is 03:45 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

Privacy / DMCA contact / Affiliate and FTC Disclosure
Copyright 2002-2013, MacRumors.com, LLC