Oscar Cabrero
Oscar Cabrero

Reputation: 4169

how to trigger button onmouseup handler from javascript

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

Upvotes: 1

Views: 16001

Answers (4)

Marko
Marko

Reputation: 72232

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>

Upvotes: 0

Metalshark
Metalshark

Reputation: 8482

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

Upvotes: 15

Castrohenge
Castrohenge

Reputation: 8993

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/

Upvotes: 8

Anders Fjeldstad
Anders Fjeldstad

Reputation: 10824

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

Upvotes: 0

Related Questions