0

I need to instead of the "Live" event in Jquery,so I must to modify my js code. But I come a problem. that is :

jquery api is : on(events,[selector],[data],fn)

But my code has some jquery object,like this:

var $obj=$(".btn");
$obj.live("click",function(){
.....................
});

so I change it:

var $obj=$(".btn");
$(document).on("click",$obj,function(){
.............................
});

the result is when I click dom ,the "click" event runing .....

Thx,my English is not good.forgive me

I have solved it ,thx for friends. "$("").selector" is a String

var $obj=$(".btn"); $(document).on("click",$obj.selector,function(){ .................... });

Free Tears
  • 347
  • 1
  • 3
  • 13
  • You forgot to use `.on()`. As in you forgot to type in the two letters 'o' and 'n' after the period. – Daedalus Nov 15 '14 at 05:01
  • You can't pass a jQuery object as the selector for the dynamic elements as it's really just a filter. – adeneo Nov 15 '14 at 05:05
  • The real question is why do you need to, if you already have the elements in a variable ? – adeneo Nov 15 '14 at 05:06
  • Your meaning is the "On" must to pass a "String" as selector? Can't pass a jquery selector object? if that ,I have no way to modify my js code ,because the js code use so many jquery object with "Live" event. – Free Tears Nov 15 '14 at 05:20
  • I'd suggest you read [this post](http://stackoverflow.com/questions/8752321/jquery-live-vs-on-method-for-adding-a-click-event-after-loading-dynamic-ht/8752376#8752376) about switching to `.on()` from `.live()` – jfriend00 Nov 15 '14 at 06:02
  • Yeah ha ha,but I need thx for stackoverflow's friends ,this post is my first post in the foreign website. – Free Tears Nov 15 '14 at 06:08

2 Answers2

2

It should be

$(document).on('click', '.btn', function() {

});

or if you already have the elements in a variable, you don't need delegated event handlers

$obj.on('click', function() {

});

You can't pass a jQuery object as the second parameter to on(), it's really just a filter that accepts a valid selector string.

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • Because my ".btn" is dynamic elements. It can "add" or "del" auto – Free Tears Nov 15 '14 at 05:11
  • But then it's not in `$obj` as that variable holds a collection of elements, not elements that can be added in the future. – adeneo Nov 15 '14 at 05:13
  • Your meaning is the "On" must to pass a "String" as selector? Can't pass a jquery selector object? if that ,I have no way to modify my js code ,because the js code use so many jquery object with "Live" event. – Free Tears Nov 15 '14 at 05:22
  • Yes, the second argument is a filter to catch dynamic elements by targeting the non-dynamic elements, and it has to be a string. What would be the point of referencing a variable with elements, when the dynamic elements aren't in that variable. – adeneo Nov 15 '14 at 05:30
  • That's one way to go, but `$().selector` is not part of the documented API and can be changed at any time, so it's not really safe to use, but it works, and has worked for years. – adeneo Nov 15 '14 at 05:49
0
var $obj=$(".btn");
$(document).on("click",$obj,function() {  // ".on" is missing
    ...
});
simonswiss
  • 36
  • 5