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
3 Answers
Reset to default 8Anytime, 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条)