I'm trying to run an attr change inside a mouseenter function. I need to make a new function within the mouseenter function because I eventually need it to callback. I'm starting from the beginning though and it's not functioning on the most basic level right now.
In the below example, you'll see that I first iterate through all images. I then take the images file names and change the suffix (that's not really important to this as far as I can tell, I'm just including it so I have everything I'm doing in here just in case). I then run a 'mouseenter' function that will fire on the image element. It logs the new 'src' of the image to the console and then should change the src. It logs the src successfully but doesn't fire the attr function successfully.
I've also logged $(this).attr('src') to make sure it's recognizing the image correctly and that works as well. So it knows the current $(this).attr('src') and recognizes the new_img_src variable yet doesn't fire the function. Why would that be?
Like I said, I eventually want to do a callback function which is why this is formatted this way. I will be called img_loop() from within img_loop() once I have all the code set up so I need the img_loop() function to be there as opposed to just using the $(this).attr() function inside mouseenter.
Thanks for the help!
$('img').each(function(){
var img_src = $(this).attr('src');
var url_prefix = img_src.substr(0, img_src.indexOf('-1.jpg'));
var new_img_src = url_prefix + '-3.jpg';
$(this).mouseenter(function(){
function img_loop(){
console.log(new_img_src);
$(this).attr('src',new_img_src);
}
img_loop();
});
});