javascript - Click event is not working for dynamically changed class in jquery - Stack Overflow

I have two class .btnn and .sleep_avail sometimes i changes the css of a anchor from .btnn to .sleep_av

I have two class .btnn and .sleep_avail sometimes i changes the css of a anchor from .btnn to .sleep_avail

Now i have two function for anchor click

 $('.btnn').click(function () {
     alert('yes btn');
 });

And Second one is

$('.sleep_avail').click(function () {
    alert('yes sleep');
});

Now when i click on the anchor with class sleep_avail which is changed dynamically from btnn class the event written for btnn raised and i get response accordingly. But what i need is that the event for sleep_avail should be raised. Kindly help.

I have two class .btnn and .sleep_avail sometimes i changes the css of a anchor from .btnn to .sleep_avail

Now i have two function for anchor click

 $('.btnn').click(function () {
     alert('yes btn');
 });

And Second one is

$('.sleep_avail').click(function () {
    alert('yes sleep');
});

Now when i click on the anchor with class sleep_avail which is changed dynamically from btnn class the event written for btnn raised and i get response accordingly. But what i need is that the event for sleep_avail should be raised. Kindly help.

Share Improve this question edited Aug 30, 2013 at 8:33 Arturs 1,2545 gold badges21 silver badges28 bronze badges asked Aug 30, 2013 at 8:30 sambit.albussambit.albus 2501 gold badge8 silver badges26 bronze badges 2
  • there are many many many posts with same issue. search in SO. – Mr_Green Commented Aug 30, 2013 at 8:32
  • A fiddle explaining the steps to reproduce the issues, and what is the desired effect would help a lot – Fabrizio Calderan Commented Aug 30, 2013 at 8:32
Add a ment  | 

3 Answers 3

Reset to default 8

Anytime, you use dynamically created tags, you must use

$(document).on('#event','#selector',function () {...});

so here

$(document).on('click','.sleep_avail',function () {...});

Because event handlers bind to the currently elements, they have to exist on the page when .on()is called

Here is the working DEMO http://jsfiddle/ARBdU/

$(document).on('click','.btnn, .sleep_avail',function () {  
 if($(this).hasClass('btnn'))
 {
    ...   
 }
 else if($(this).hasClass('sleep_avail'))
 {      
   ...
 }
});

try

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744777759a4593146.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信