How to show image before executing javascript?

How show and hide an image using Javascript ?

  • I have 2 little divs with 2 little images, the idea is to click over one of this images in order to see the "normal version" of the same image in a third div (to see the image bigger). How in the name of hell and heaven I can do that ? Any idea ? Greatings and thanks

  • Answer:

    You could try using a lightbox for example, they are quite popular with the web 2.0 crowd due to their ease of use and user friendlyness. http://www.huddletogether.com/projects/lightbox2/ If not, you could look into using jQuery for your own custom needs (look up the .focus() function here: http://api.jquery.com/focus/ for a starter)

renzocj at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

Let's make your life easier on writing javascript. We will use jquery library on this matter. First download the jquery library, its about 8kb and include it on your page. Here's how to include <script type="text/javascript" src="jquerylocation"></script> below this code we will create javascript to manipulate images. <script type="text/javascript"> $(document).ready(function(){ //#img1 is the id of your image $("#img1").click(function(){ $("this").attr("src", "srcofbiggerimage.jpg"); //this code replace the image into bigger image, note that srcofbiggerimage.jpg is the source of your image }); //to return to original image on mouse out $("#img1").mouseout(function(){ $("this").attr("src", "srcofsmallimage.jpg"); //this code replace the image into bigger image, note that srcofbiggerimage.jpg is the source of your image }); }); </script> There's a lot of stuff including clean animations a replacement technique you can use in Jquery. Just contact me, I can offer free training. I also offer web development services.

document.getElementById("your div's id").style.display = none; to hide it. Change none to inline to show it.

OK if I am correct you have something like the following HTML code on your page: <div><img src="largeImg1.jpg" id="largeImg"></div> <div><img src="thumbnail1.jpg" id="thumb1Img"></div> <div><img src="thumbnail2.jpg" id="thumb2.img"></div> There maybe more or less coding but that is the rudements of what you need a simple way to do this is to change it to: <div><img src="largeImg1.jpg" id="largeImg" /></div> <div><img src="thumbnail1.jpg" id="thumb1Img" onmouseover="document.getElementById( 'largeImg').src ='largeImg1.jpg'" /></div> <div><img src="thumbnail2.jpg" id="thumb2.img" onmouseover="document.getElementById( 'largeImg').src='largeImg2.jpg'" /></div> This will make the large image change when aa users mouse enters the smaller thumbnail. Also if the user doesn't have javascript enabled they will at least be able to see the large image of the first thumbnail. I would also advise to have an onload event preloading the images this can be added to the body tag as follows: <body onload="img1=new Image(); image1.src='largeImg1.jpg'; img2=new Image(); image2.src='largeImg2.jpg';"> The advantage of preloading is that one the mouseover the image will change instantly rather than have to download the image the first time it happens.

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.