ak0053792
ak0053792

Reputation: 563

Show Image for a field on mouse over

I want to show an image depending on the value of a field on mouseover. I have searched it in here, and tried some thing like below. But the problem is that i can't use #image as there is no placeholder like that.

 $("#s_1_1_60_0_icon").mouseover(function () {
    $(*'#image'*).show(); //displays image on mouse in
 }
 $("#s_1_1_60_0_icon").mouseleave(function(){
    $(*'#image'*).hide(); //hides image on mouse out
});
});

I want to show an image like a tooltip. Please suggest a direction to achieve this.

Thanks

Upvotes: 0

Views: 195

Answers (1)

Snowburnt
Snowburnt

Reputation: 6922

Here's a simple one based on your fiddle. http://jsfiddle.net/RnnrA/

Like with most things in web design there's millions of ways to do something. If you can give us more details (where is the image, for example) I could give you a better way of doing it.

$("li a").mouseover(function(){
    $("#img_"+$(this).attr("id")).show();
});
$("li a").mouseout(function(){
    $("#img_"+$(this).attr("id")).hide();
});

This one's a little more complex: http://jsfiddle.net/e7UVb/

I made a div that holds the image and everytime you mouse over it creates an image element and populates it with an image then appends it to the div. on mouseout it destroys the image element. This will require more client calls because everytime you mouse over it needs to go get the image. In my example I only used one image, you'll have to use some better logic to figure out which image to display for this one.

$("li a").mouseover(function(){
    $("#imagePlaceholder").append($("<img></img>").prop("src","http://www.psdgraphics.com/file/psd-credit-card.jpg").prop("height","50"));
});
$("li a").mouseout(function(){
    $("#imagePlaceholder").empty();
});

Finally, a much simpler version: http://jsfiddle.net/Zt2pu/1/

$("li a").mouseover(function(){
    $("#imagePlaceholder").prop("src","http://www.psdgraphics.com/file/psd-credit-card.jpg").prop("height","50");
});
$("li a").mouseout(function(){
    $("#imagePlaceholder").prop("src","");
});

Here I just made an empty image tag, and on mouseover I changed the src attribute. On mouse out I emptied the src attribute. Just like the previous one, you'll have a client call to download the image on every mouse over.

Bottom line, the top one is the simplest and easiest. The client will load all of the images once and only once so there will only be the initial call to download all the images.

again, give us a better idea of what you're actually doing and we can give you a better answer on how to accomplish that. In the meantime, have fun with this.

Upvotes: 1

Related Questions