O'Reilly logo

Programming Reactive Extensions and LINQ by Paul Betts, Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Integrating RxJS with jQuery DOM Events

Let's see how we can take DOM events such as ‘keyUp’ or ‘blur’, and create an Observable based on them, similar to how we use Observable.FromEvent in .NET. The easiest way to do this is via the RxJS jQuery adapter (included with RxJS as the file, rx.jQuery.js), which is a jQuery plug-in that adds extra methods onto jQuery that make using RxJS alongside jQuery a snap!

RxJS adds a new method called toObservable(), which converts a DOM event to an Observable. The following is a simple example:

var keyDownEvent = $(window).toObservable("keydown");

The Type of the data provided by the Observable depends on the DOM event—for keyDown, the type is KeyboardEvent, which has a property called ‘keyCode,’ which we ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required