0

I have tried different approaches to get this to work. I know I have to wrap the function but can not get it to work. The function is running in a .js file.

http://jsfiddle.net/hookedonweb/yq8Hz/

function showLabel(labelName) {
   var labelImgs = jQuery(".content .packaging .labels img");
   var focusImg  = jQuery("#label_" + labelName);

   if (labelImgs.length > 0 && focusImg.length > 0) {
       labelImgs.each(function() { jQuery(this).removeClass("active"); });

       focusImg.addClass("active");
   }
}
2
  • 1
    Working fiddle for those curious. Commented Nov 3, 2012 at 5:14
  • Given your function works.. the only things I can assume are that you are not including jquery in your page, and that your function is not in the document head. Commented Nov 3, 2012 at 5:19

1 Answer 1

1

I think you may be confusing PHP and jQuery. Anyways, your code can be simplified a lot. This seems to do what you want. If it doesn't then you need to clarify your question.

http://jsfiddle.net/yq8Hz/3/

Don't use inline javascript. You are using jQuery, so use it to separate your code from your html. This is a good practice. You can use the data attributes to store the img you want to reference.

<div><a href="" data-label="macandcheese">See Label</a></div>
<div><a href="" data-label="lasagna">See Label</a></div>
<div><a href="" data-label="beefstroganoff">See Label</a></div>

Then use jQuery's hover to add/remove the active class. We make use of jQuery's data method to get the label we want.

$('a').hover( function() {
    $('img').removeClass('active');
    $('img#label_' +  $(this).data('label')).addClass('active');
});​
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.