最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

event handling - how to trigger button onmouseup handler from javascript - Stack Overflow

programmeradmin1浏览0评论

how to trigger onmouseup handler from javascript

i have my button

<input id="x" onmouseup="doStuff()">

and i want to trigger

document.getElementById("x").onmouseup();?????

how to trigger onmouseup handler from javascript

i have my button

<input id="x" onmouseup="doStuff()">

and i want to trigger

document.getElementById("x").onmouseup();?????
Share Improve this question asked Jul 13, 2010 at 8:04 Oscar CabreroOscar Cabrero 4,1698 gold badges31 silver badges49 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 15

If you want to use event binding there is always this way of doing things

document.getElementById("x").addEventListener("mouseup", function(){
    alert('triggered');
}, false);​

Here is the example JSFiddle of it.

Or if you want to actually "Trigger the event" try this

var evt = document.createEvent("MouseEvents");
evt.initEvent("mouseup", true, true);
document.getElementById("x").dispatchEvent(evt);

You've already answered your question :)

Simply call

document.getElementById("x").onmouseup()

in your code.

I've added an example here:

http://jsfiddle.net/cUCWn/2/

How about binding the mouseup event of "x" to a function, and then call that function from doStuff?

You pretty much have the answer there.

<input id="x" onmouseup="doStuff()" />

and then just declare your function

<script type="javascript">
    function doStuff() {
        alert("Yay!");
    }
</script>

Alternatively, you can attach the event from Javascript. i.e.

<script type="text/javascript">
    var x = document.getElementById("x");
    x.onmouseup = doSomething;

    function doSomething() {
        alert("Yay!");
    }
</script>
发布评论

评论列表(0)

  1. 暂无评论