0

DEMO

$(function(){
    $('button').click(function(){
       $('p').addClass('red');


    });

    $('body').on('.red','click',function(){
        alert(''); // doesn't work?
    });
});

Why does the red class's click event doesn't fire? I thought by using on() it will catch any future coming classes?

Is it because of closure problem?

RatDon
  • 3,403
  • 8
  • 43
  • 85
Elton Jamie
  • 586
  • 6
  • 17

3 Answers3

0

You have incorrect syntax for event delegation:

$('body').on('click','.red',function(){
    alert('test'); 
});

Working Demo

Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
0

USe

 $('body').on('click','.red',function(){
        alert(''); // doesn't work?
    });

You misplaced class name and event..

Fiddle

Anoop Joshi P
  • 25,373
  • 8
  • 32
  • 53
0
Syntax error
$('body').on('click','.red',function(){
    alert(''); 
});
--OR--
$(document).on('click', '.red',function(){
    alert(''); 
 });
Namit
  • 21
  • 2