Solved

jQuery find matched class

Posted on 2014-12-02
3
199 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 43

Accepted Solution

by:
Rob 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

690 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