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,222
  • 7
  • 38
  • 79
Elton Jamie
  • 586
  • 4
  • 17

3 Answers3

0

You have incorrect syntax for event delegation:

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

Working Demo

Milind Anantwar
  • 79,642
  • 23
  • 92
  • 120
0

USe

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

You misplaced class name and event..

Fiddle

Anoop Joshi P
  • 24,863
  • 8
  • 29
  • 52
0
Syntax error
$('body').on('click','.red',function(){
    alert(''); 
});
--OR--
$(document).on('click', '.red',function(){
    alert(''); 
 });
Namit
  • 21
  • 2