João Louros
João Louros

Reputation: 2760

How can I fire an event in WinJS (equivalent of .trigger("") in jQuery)

I would like to know how can I fire an event from code. In jQuery to fire a "onchange" event you would simply $("selector").trigger("change"). Is there a way to do the same using WinJS.

[All suggestions of including jQuery will be discarded]

Thanks.

Upvotes: 1

Views: 2024

Answers (4)

pdschuller
pdschuller

Reputation: 584

To fire and respond to a custom event do this.

fire the event

WinJS.Application.queueEvent({type: 'ev_login_completed'})

to respond to the custom event

WinJS.Application.addEventListener('ev_login_completed', function (e) {
             // do stuff  })

HTH

Upvotes: 0

Valentin Kantor
Valentin Kantor

Reputation: 1844

You can use event.initUIEvent:

var button = document.getElementById('button');
var еvent = document.createEvent("UIEvents");
еvent .initUIEvent("click", false, false, window, 1);
button.dispatchEvent(еvent );

More info can be found here

Upvotes: 0

samuelesque
samuelesque

Reputation: 1165

In your ready function:

WinJS.Utilities.query("#yourSelector").listen("change", this.customClicked, false)

And then define customClicked function (can be named anything)

customClicked: function(eventInfo){
   <!--do stuff -->
}

Upvotes: 0

peterm
peterm

Reputation: 92785

fireEvent is probably what you want.

Fires a specified event on the object.

Given that markup

<p>
    <button 
        id="oButton" 
        onclick="this.innerText='I have been clicked!'">Button
    </button>
</p>

You can do

oButton.fireEvent("onclick");

Upvotes: 3

Related Questions