Solved

jQuery find matched class

Posted on 2014-12-02
3
188 Views
Last Modified: 2014-12-02
Hello Experts,

This should be fairly simple, I hope.
I have a nav-menu on a page and few photos. I included the same classes for both so it can be easily matched.
What I am trying to do is... when you mouse-over the the menu link with a class of "artist" the photo with the same class will highlight (by adding an active class to it). and vise verse.


// the menu //

<ul>
  <li><a href="" class="link artist">Artist</a></li>
  <li><a href="" class="link dog">Dog</a></li>
</ul>

// the photos //

<a href="" class="photo1 artist"><img src="image1.jpg" alt=""></a>
<a href="" class="photo2 dog"><img src="image2.jpg" alt=""></a>

Open in new window

0
Comment
Question by:Refael
3 Comments
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 250 total points
ID: 40476968
Here is your demo: http://jsbin.com/sedori/1/edit?html,css,js,output

$(function() {
	$('li a').hover(function() {
		//in
		var mytype = $(this).data('type');
		$('.photo.'+mytype).addClass('active');
	},
					function() {
		//out
		var mytype = $(this).data('type');
		$('.photo.'+mytype).removeClass('active');
	}
				   );
});

Open in new window


I changed a couple of things.  Firstly, changed the class photo[X] to just photo.  Secondly, I'm utilising the data attribute to store the data, rather than in the class.  This is a cleaner way to do it.
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 250 total points
ID: 40476979
Test page : http://jsfiddle.net/2xh4h8t4/

$("ul a.link").hover(function() {
    var myclass = $.trim($(this).attr("class").replace("link",""));
    $("."+myclass+"[class^=photo]").show();    
}, 
function() {
    var myclass = $.trim($(this).attr("class").replace("link",""));
    $("."+myclass+"[class^=photo]").hide();    
});

Open in new window

0
 

Author Closing Comment

by:Refael
ID: 40477062
Rob Jurd and leakim971, Thank you so much!

I tried both solutions and both works just great :-) Thank you both for such a quick reply!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now