Pass value to JQuery hover function

Hi, I'm to the javascript and jquery world, the question may seem a bit dummy

On my html, let's say I have two divs and two images

<div id="div1">
   <img src="abc.jpg" id="img1" />
</div>
<div id="div2>
   <img src="def.jpg" id="img2" />
</div>

In Jquery, I have the following code:
$('#div1').hover(function () {
        $("#img1').fadeIn("slow");
    }, function () {
        $("#img1').fadeOut("fast");
    });

$('#div2').hover(function () {
        $("#img2').fadeIn("slow");
    }, function () {
        $("#img2').fadeOut("fast");
    });

obviously, there should be an easier way to write the code, like pass some values to the jquery instead make two similar functions, can some one shorten the jquery function for me?
tks
LVL 3
joeyluAsked:
Who is Participating?
 
hieloCommented:
Then give your divs a common class and use that class instead:

<div id="div1" class="img">
   <img src="abc.jpg" id="img1" />
</div>
<div id="div2" class="img">
   <img src="def.jpg" id="img2" />
</div>
$('.img').hover(function () {
    var id='#' + this.id.replace('div','img');
        $(id).fadeIn("slow");
    }, function () {
        $(id).fadeOut("fast");
    });

Open in new window

0
 
Gurvinder Pal SinghCommented:
you can try making a wrapper around this method

function fnDivImageHover(divId, imageId)
{
$('#' + divId).hover(function () {
        $('#' + imageId ).fadeIn("slow");
    }, function () {
        $('#' + imageId ).fadeOut("fast");
    });

}
0
 
hieloCommented:
$('#div1,#div2').hover(function () {
    var id='#' + this.id.replace('div','img');
        $(id).fadeIn("slow");
    }, function () {
        $(id).fadeOut("fast");
    });

0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
joeyluAuthor Commented:
hi Qurvider372, with your code, how am going to pass the divId, imageId, or how do I call this fnDivImageHover function? sorry again, not too familiar with javascript

hi hiedo, your function works, but since I have more than 10-15 divs, like div1, div2,... div15, I don't think hard coding them into the jquery selector is an ideal way, could the div and img IDs been passed by some thing else?

anyway, tks for both of you
0
 
Gurvinder Pal SinghCommented:
just call it like this

fnDivImageHover( 'div1', 'img1');
fnDivImageHover( 'div2', 'img2');

refer to this link on how to call a javascript function
http://devlicio.us/blogs/sergio_pereira/archive/2009/02/09/javascript-5-ways-to-call-a-function.aspx
0
 
joeyluAuthor Commented:
hi qurvinder372

Since this is a jquery hover function, onClick="function()" is not going to work, and I tried use onmouseover to call the function,  not work either. maybe I did something wrong, but anyway, tks for your help
0
 
joeyluAuthor Commented:
tks, that worked
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.