views:

40

answers:

4

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();?????
A: 

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

Anders Fjeldstad
+1  A: 

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/

Castrohenge
A: 

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>
Marko
A: 

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);
Metalshark