views:

48

answers:

2

Hello. I have an image on my website. I would like when I click it, it show a <div>, and when I click it another time, it hides the <div>. How can I do this in JavaScript? Should I use jQuery?

+1  A: 

Have a look at

astander
+3  A: 

You can do like this in JQuery:

<img id="img_id" src="image path here" />
<div id="mydiv" style="display:none;">Some content</div>

$('#img_id').toggle({ 
   function(){$('#mydiv').show();},
   function(){$('#mydiv').hide();}
});

First click on an element with id img_id will show an element with id mydiv and second click would hide it.

See more info about toggle() function.

Sarfraz
And, don't I have to link the image?
Francesc
@Francesc: As can be seen the `img_id` is supposed to be for an image. Thanks
Sarfraz
@Francesc: please see my updated answer. Thanks
Sarfraz