Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Using Jquery to select multiple targets.

Featured Replies

In jquery, how would i go about targeting multiple elements.

 

For example, say i have 4 boxes.

 

[a]____[c]__[d]

 

And when i mouseover box [c] i want to apply an effect to boxes a,b and d

 

But if i mouse over box [a] i want it to apply to b,c and d.

 

How would i go about this?

 

I've tried a few ideas i had, but they just plain and simple didn't work. I have a very bad habbit of overcomplicating things though, so would like some input.

 

Suggestions, pointers, links, anything welcome

add them all in a div containing them give it class="container"

 

each one has its own class

 

$('div.container > div').hover(function() {

var notHovered = $(div.container > div').not($(this).attr("class"));

 

notHovered.slideTo(); // or whatever other effect you want to apply

});

 

May not work, quick idea though =]

  • Author

Thank you, that was a massive help

 

now i've evolved it into this (please bear with me as i'm still a jquery noob/novice)

 

$(window).bind('load', function () {
$('div.case-study-list > a').hover(function() {
var notHovered = $('div.case-study-list > a').not($(this).attr("class"));
notHovered.fadeOut("3000"); // or whatever other effect you want to apply
});
});

 

now that fades it out, but firebug flags up

handler is undefined

 

plus now i've got that to fade out, how do i get it to fade back in when the mouse is no longer hovering?

$(window).bind('load', function () {

$('div.case-study-list > a').hover(function() {

var notHovered = $('div.case-study-list > a').not($(this).attr("class"));

notHovered.fadeOut("3000"); // or whatever other effect you want to apply

}, function() {

var notHovered = $('div.case-study-list > a').not($(this).attr("class"));

notHovered.fadeIn("3000"); // or whatever other effect you want to apply

});

});

 

 

 

That should do the trick

  • Author

Almost there now, thank you. Starting to wish i found this forum sooner if everyone's as helpful as you :p i just hope iEntry never buy it.

 

Anyway, that loops the animation, so how would i go about having it break after it's faded out, and another brake when it's faded back in?

  • Author

Ok, i've been fiddling around with it, i've got the fade out for when the mouse is over it, and the fade back in for when the mouse leaves, but it's doing all of my images, not just the ones not hovered on....

 

Anyone help?

 

 

$(window).bind('load', function () {
$('div.case-study-list > a > img').hover(function() {
var hover = $('div.case-study-list > a > img').not($(this).attr("class"));
hover.animate({opacity: 0}, 250); // or whatever other effect you want to apply
}, function() {
$('div.case-study-list > a >img').not($(this).attr("class"));
var notHovered = $('div.case-study-list> a > img');
notHovered.animate({opacity: 1}, 250); // or whatever other effect you want to apply
});
});

  • Author

Ok, i've got it pretty much fully working, there's just one minor thing i want to fix.

 

Can anyone help me set it up so that if when it's triggered again it waits until the animation has finished, before starting the image on the next image?

 

And for anyone who's interested, the working code is.

 

$(window).bind('load', function () {
$('div.case-study-list > a > img').hover(function() {
var hover = $('div.case-study-list > a > img').not(this);
hover.animate({opacity: 0}, 250); // or whatever other effect you want to apply
}, function() {
$('div.case-study-list > a >img');
var notHovered = $('div.case-study-list> a > img');
notHovered.animate({opacity: 1}, 250); // or whatever other effect you want to apply
});
});

 

 

With this html

 

		
<div id="case-study">	
 <div class="case-study-list"><a href="#" /><img src="image.jpg" alt="image description"></a></div>
 <div class="case-study-list"><a href="#" /><img src="image.jpg" alt="image description"></a></div>
 <div class="case-study-list"><a href="#" /><img src="image.jpg" alt="image description"></a></div>
 <div class="case-study-list"><a href="#" /><img src="image.jpg" alt="image description"></a></div>
</div> <!-- end of case-study -->

 

That's there for reference so it's had all my code stripped out except for what is actually needed for the effect ready if anyone should ever want to use this for there own work.

 

It's been a good learning curve (first time i've actually done something myself with jquery), and thanks for your help.

  • 3 weeks later...

So i added this to your code

 

$(window).bind('load', function () {

$('div.case-study-list > a > img').hover(function() {

var hover = $('div.case-study-list > a > img').not(this);hover.stop().animate({opacity: .5}, 550);

}, function() {

$('div.case-study-list > a >img');

var notHovered = $('div.case-study-list> a > img');notHovered.stop().animate({opacity: 1}, 550);

});

});

 

Is that the desired effect u were looking for?

 

This little piece of java script has really helped me out on a personal project. Thank u so much!

  • Author

I worked that out, should of updated it sorry. It was a good learning curve (: Wether it's done the best way or not i still don't know.... but i'm learning (:

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.