📜 ⬆️ ⬇️

live: new way to set an event handler

image As you know , the beta version of jQuery 1.3 has recently been released. While it is raw, it is being tested, and there are still unpleasant bugs in it. But getting acquainted with innovations is worth it and I would like to briefly talk about one of them.

The "live" function is an analogue of the bind function, syntactically and essentially identical, but there is one huge difference between them that is best illustrated by the following example.

Try the demo . Explanations under the cut.
')

In the example, two sets of buttons, pressing the first is handled by the function assigned through bind, the second through live.
When adding buttons dynamically, we can observe the main difference: in the first set, new buttons do not receive a handler, in the second a handler is automatically added for each new button.

And although the differences in the code are small:
$( "input.test" ).bind( "click" , function () {
alert( "hello base input!" );
});
$( 'input.lcmd' ).live( "click" , function () {
alert( "hello live input!" );
});

* This source code was highlighted with Source Code Highlighter .

in fact, we have a huge difference.

PS: previously similar functionality could be obtained through this plugin .

PPS: during testing I ran into a bug that shows how much 1.3 more cheese:
if you specify a complex selector $ ('# lblock input.lcmd'). live (..., then the handler will not hang at all, and, when replaced with bind, everything works fine. We are waiting for corrections in future versions.

PPPS: but $ ('input.lcmd', $ ('# lblock')). Live (... works, which, in general, is not surprising

Source: https://habr.com/ru/post/47822/


All Articles