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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.