admin管理员组文章数量:1333630
I need to simulate a click or mouse event. I tried various things but the lib i am using doesnt seem to respond to it or does respond but only on specific browsers. ATM i do $('#target').val($('#target2').val()); which works on firefox and opera. Fails on chrome, IE8 and safari.
I could add events to the libs but i wouldnt know which event to add (or how to do it properly). Anyways how do i solve this? basically i am setting the textarea text with .val() and the lib doesnt seem to pick up that event.
I need to simulate a click or mouse event. I tried various things but the lib i am using doesnt seem to respond to it or does respond but only on specific browsers. ATM i do $('#target').val($('#target2').val()); which works on firefox and opera. Fails on chrome, IE8 and safari.
I could add events to the libs but i wouldnt know which event to add (or how to do it properly). Anyways how do i solve this? basically i am setting the textarea text with .val() and the lib doesnt seem to pick up that event.
Share Improve this question edited Jan 18, 2011 at 14:28 jAndy 236k57 gold badges312 silver badges363 bronze badges asked Jan 18, 2011 at 14:15 user34537user34537 3-
What you mean by "libs"? What library? What you expect to happen when you set the textarea text?
$('#target').val($('#target2').val());
got nothing to do with click or keydown event, it's only setting value of something to be equal to value of something else.. – Shadow Wizzard Commented Jan 18, 2011 at 14:21 - @Shadow Wizard: yes and as i mention the library i am using doesnt pick up the event. I need to trigger the event for the code to work correctly – user34537 Commented Jan 18, 2011 at 14:25
- yes to what? You didn't answer any of my questions and none was Yes/No.. – Shadow Wizzard Commented Jan 18, 2011 at 14:34
3 Answers
Reset to default 3You could use the DOM Level 2 Event Model, like:
function simulateClick(element) {
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
cb.dispatchEvent(element);
}
Demo: http://www.jsfiddle/4yUqL/66/
This will truly simulate a mouseclick on an element. Regardless how events were bound.
.trigger('click')
in jQuery might achieve what you're trying to do. It will fire all the handlers attached to the click event.
In case anyone bumps into this looking for a framework agnostic way to fire any HTML and Mouse event, have a look here: How to simulate a mouse click using JavaScript?
本文标签: javascriptHow do i simulate a click or keydown eventStack Overflow
版权声明:本文标题:javascript - How do i simulate a click or keydown event? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742343070a2456986.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论