Website has the following code:
<ul class="left">
<li><div id="run" class="button"><div class="inner">Run</div></div></li>
</ul>
And this script:
$("#run").click(function() {...});
The following code doesn't work!:(
document.getElementById('run').click();
How to press the button "Run" by javascript mand to browser?
Thanks a lot for help!
Website has the following code:
<ul class="left">
<li><div id="run" class="button"><div class="inner">Run</div></div></li>
</ul>
And this script:
$("#run").click(function() {...});
The following code doesn't work!:(
document.getElementById('run').click();
How to press the button "Run" by javascript mand to browser?
Thanks a lot for help!
Share Improve this question edited Dec 4, 2011 at 20:59 Dmitry asked Dec 4, 2011 at 20:10 DmitryDmitry 14.6k24 gold badges112 silver badges194 bronze badges 3- Do you want to dynamically click the button (without mouse click) or do you want to add a click event to the div? – Will Commented Dec 4, 2011 at 20:15
- The second block of code was jQuery; are you actually using jQuery, or do the answers need to be in pure/plain/vanilla JavaScript? (If jQuery's acceptable, consider adding the jquery tag.) – David Thomas Commented Dec 4, 2011 at 20:20
- jQuery is in the following file: <script type="text/javascript" src="scripts/main.js"></script>. I need to run javascript mand by browser. – Dmitry Commented Dec 4, 2011 at 20:58
5 Answers
Reset to default 6Quite simple to do with jQuery (which it appears that you are already using):
$("#run").click();
As they say, RTFD: http://api.jquery./click.
Try:
$('#run').trigger('click');
Good old Javascript:
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
var run = document.getElementById("run");
run.dispatchEvent(evt);
In jQuery:
$("#run").click();
More docs aboute firing mouse events manually.
$("#run").click(function() { /* ... */ }).trigger('click');
Add onclick="run()"
to the outer div tag.
You may want to add onselectstart="return false"
to avoid selecting the text.