-2

This might be a simple question but in some cases when I dynamically create elements .on doesn't work but live works.

I know its better to use .on, but what am I doing wrong?

This works for me:

    $('.btnMore').live('click', function () {
    // do something
    }

This doesn't work for me:

    $('.btnMore').on('click', function () {
    // do something

    }

How can I make the .on work at all times, like .live?

Kim
  • 1,128
  • 6
  • 21
  • 41

3 Answers3

0

I found the solution:

This works:

$(document).on('click', '.btnMore', function () { // do something }

Kim
  • 1,128
  • 6
  • 21
  • 41
0

jquery on method works since jquery version 1.7 see on documentation

Nick
  • 4,192
  • 1
  • 19
  • 30
0

See this blog for the differences between .on() and .live()

but it looks like instead of

$('selector').live(event, function(){ //do stuff here }) 

you want

$('selector').live(event, function(){ //do stuff here })
Envin
  • 1,463
  • 8
  • 32
  • 69