1

I understand that this question has been answered before, but even after consulting those links, I am still unable to solve my problem. I want to replace my image (assets/img/social-mail.jpg) to another (assets/img/social-mail-hover.jpg), with a single class, because I have multiple images I would like to do this to. My basic thought process is this: When the class is hovered, take the ID, and replace its image with another by adding "-hover" to its image link.

HTML:

<img id="social-mail" class="box-social" src="assets/img/social-mail.jpg">

JS:

$(".box-social").hover(function(e) {
    var id = $(this).attr("id");
    var icon = id.split("-")[1];
    var image = "img/social-" + icon + "-hover.jpg";
    $(id).find("img").attr("src", image);
});

I've tested id, icon, and image; and they all give me exactly what I want. However, when I hover over the image, it still isn't being replaced. I'm not getting any errors either. What am I doing wrong? Thank you so much!

8
  • It would need to be $(this) or $('#'+id) to make it a proper id selector... but use image sprites and CSS for this! Commented Jan 10, 2015 at 5:18
  • You mentioned the path as "assets/img/.." but in script you are doing: var image = "img/social-" + icon + "-hover.jpg"; . Are you sure you are not missing "assets" in the path? Commented Jan 10, 2015 at 5:19
  • Why are you using .find()? this already refers to the current img. Don't use .attr() either: this.src = image; should do it. (Also, why are you using .split() to take the part after the hyphen when the part before the hyphen is actually part of the filename anyway? Can't you do it all with one line: this.src = "assets/img/" + this.id + "-hover.jpg";? Commented Jan 10, 2015 at 5:21
  • @numbers1311407 just added that, and it didn't work. It works if I purely use CSS with :hover, but I have six images and wanted to try this with JQuery. Commented Jan 10, 2015 at 5:23
  • @nnnnnn has it correct. this is the element, you don't need to find anything. Regardless this is a typical scenario for CSS and a single image sprite that holds all your images. No JS required. Commented Jan 10, 2015 at 5:24

3 Answers 3

2

I think there are at least four problems with your function:

  1. To select by element id you need the # prefix, so $("#" + id) not $(id).

  2. You don't want .find() because this is already the image in question.

  3. Your image path in the html begins with "assets" but you don't include that in your Javascript.

  4. If you only supply one funtion to .hover() that function will be called both when the mouse moves in and when it moves out. So you never change the image back.

The four lines of your function can be replaced with one line:

$(".box-social").hover(function(e) {
  this.src = "assets/img/" + this.id + "-hover.jpg";
});

This fixes problems 1-3 above. To fix problem 4 just supply a second function to change the src back when the mouse leaves:

$(".box-social").hover(function(e) {
  this.src = "assets/img/" + this.id + "-hover.jpg";
}, function(e) {
  this.src = "assets/img/" + this.id + ".jpg";
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for your help! This does indeed solve all my problems; I definitely should have asked about this way earlier.
0

You need to correct your selector. JS:

$(".box-social").hover(function (e) {
    var id = $(this).attr("id");
    var icon = id.split("-")[1];
    var image = "assets/img/social-" + icon + "-hover.jpg"; //make sure the path is correct
    $('#' + id).attr("src", image); //Changed the selector.
});

Demo:http://jsfiddle.net/GCu2D/521/

1 Comment

thank you for your help! I was a bit confused because my js file was in assets, and so I was debating whether to include assets or not. I guess because I was replacing something in my index file, I had to include assets.
0

try this

$(".box-social").hover(function(e) {
    var id = $(this).attr("id");
    var icon = id.split("-")[1];
    var image = "assets/img/social-" + icon + "-hover.jpg";
    $("#"+id).attr("src", image);
});

you forgot "assets/" in your image link and your img selector is not corresct

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.