Friday 15 July 2011

How to select new element in jquery? -



How to select new element in jquery? -

why alert not work?

$('.checkprice').click(function(el){ $(this).addclass('checkcheck'); }); $( '.checkcheck' ).on( "click", function() { alert('yes'); });

may .checkcheck creates more longer, .on() start?

waht must do?

jquery setup event handlers on matched elements. instance, next code apply element has .checkcheck class @ time code ran, , not @ future time:

$( ".checkcheck" ).on( "click", handlecheck );

if class added element @ future time, handler will not handle click events it, since didn't have class @ time $.fn.on called originally.

the popular way around issue leverage event delegation, utilizes bubbling-nature of javascript events. rather placing handler straight on .checkcheck elements, can place 1 farther dom tree:

$( document ).on( "click", ".checkcheck", handlecheck );

now don't care if element has .checkcheck class when script ran, care if has class when event bubbles document. means can add together brand new elements dom, given them .checkcheck class, , trigger handlecheck when clicked.

jquery

No comments:

Post a Comment