views:

353

answers:

2

How to emulate an mousedown event using javascript? See, i got a link on a page smth like this:

<a class="boo" target="_blank" onmousedown="function_rc()" href="http://www.siteaddress.com" tabindex="2"></a>

and i have to dispatch onmousedown event in a programm way, after user makes some actions. for an example i tried this:

<html>
<head>
<script type="text/javascript">
function showChar()
{
alert('mouse down');
}
function fake()
{
var evt = document.createEvent('MouseEvents');
evt.initMouseEvent ('mousedown', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
document.getelementbyid('my_link').dispatchEvent(evt);
}
</script>
</head>
<body>
<a id="my_link" onmousedown="showChar()" >LLL</a>
<div id="alert" onclick="fake()">Click me!</div>
</body>
</html>

if i understand it correctly - when i click on div with id="alert" function start working, create an MouseEvent evt type=mousedown and dispatc this event on a with id="my_link". So after the evt is dispatched function showChar() should start. but it doesn't happen. who knows why and how to solve this situation?

A: 

If you use jquery you could just do

$("#my_link").click();
sberry2A
this does not work in my situation, cause i need to emulate not onClick event, but only OnMouseDown!...=(
lazybob
A: 

so, the disicion was very simple (thanx to sberry2A)

$("#my_link").mousedown();

if i use it - everything fires as it should=) p.s.\maybe it's simple for u, but i prefer asm, and js for me - better kill myself=)

lazybob

related questions