Solved

jQuery find matched class

Posted on 2014-12-02
3
193 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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)

776 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