Simulate Click Javascript

Posted by Belgin Fish on Stack Overflow See other posts from Stack Overflow or by Belgin Fish
Published on 2010-04-24T18:10:30Z Indexed on 2010/04/24 18:13 UTC
Read the original article Hit count: 175

Filed under:

Hi, I'm just wondering how I can have javascript simulate a click on a element.

Currently I have

<script type="text/javascript">
function simulateClick(control)
{
    if (document.all)
    {
        control.click();
    }
    else
    {
        var evObj = document.createEvent('MouseEvents');
        evObj.initMouseEvent('click', true, true, window, 1, 12, 345, 7, 220, false, false, true, false, 0, null );
        control.dispatchEvent(evObj);
    }
}
</script>

<a href="http://www.google.com" id="mytest1">test 1</a><br>

<script type="text/javascript">
    simulateClick(document.getElementById('mytest1'));
</script>

But it's not working :(

Any ideas?

© Stack Overflow or respective owner

Related posts about JavaScript