user1481850 user1481850 - 6 months ago 16
Javascript Question

hide/show div on image click with jquery?

I am trying to show/hide div when click on an image. when I click on the image nothing happens. What is wrong with my code

<script type="text/javascript">
$('#close').click(function(){
$('#main').show();
$('#login').hide();
});
</script>

<div style="float:right;"><a href="#">Close <img id="close" src="assets/close.png"></a></div>

Answer
<script type="text/javascript">
    $(function(){
        $('#close').live('click',function(){
            $('#main').show();
            $('#login').hide();
        });
    });
</script>

<div style="float:right;"><a id="close">Close <img src="assets/close.png" /></a></div>

Use the live function. Also, I would target the a tag so the text also triggers it.