?
Solved

jQuery find matched class

Posted on 2014-12-02
3
Medium Priority
?
205 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 1000 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 1000 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

Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
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)
Suggested Courses

801 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