April 29, 2012

Mousedown event not fired on a Div if the Div is placed on top of an img tag – issue is only in IE

Question by user983327

I have placed a Div on top of an img tag. In my example the Div with cls ‘justForClickCls’ is placed on top of an img which is wrapped in another div ‘gameBoardElementGreyCls’. The mousedown on the ‘justForClickCls’ div is not firing in IE. However it works in every other browser.

Here is the code. Any help is much appreciated. By the way i’m testing it i IE 9

<div class='gameBoardElementCls'>
    <div id='9' class='justForClickCls' style='z-index:10;position:absolute;margin:20px;width:80px;height:80px;'></div>
    <div class='gameBoardElementGreyCls'><img src='img/greygmelement.png' width='110px' height='110px'></img></div>
</div>

<script>
    $("div.justForClickCls").mousedown(function () {
        alert('clicked 1');
    });
</script>

Answer by Starx

First thing you should fix is the closing tag in the img

<img src='img/greygmelement.png' width='110px' height='110px'></img>

To

<img src='img/greygmelement.png' width='110px' height='110px' />

Next, mousedown seems unnecessary since you are trying to trap the click. Simple use click instead of mousedown.

Now check this demo of you coding, which is working

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!