A
A
Alexander2017-01-20 14:08:08
JavaScript
Alexander, 2017-01-20 14:08:08

Is each needed on click event (simple code inside)?

Elementary question.
Both codes work fine, but which one is better? I don't understand if each is needed?

$('.apply_button, .canget').click(function(e) {
// код
});


$('.apply_button, .canget').each(function(){
        $(this).click(function(e) {
// код
        }):
}):


There are many buttons with both classes on the page.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
iBird Rose, 2017-01-20
@sanok_ps

when clicking each, in any case, it will be superfluous. if the elements on the page were originally - use the first click option. if they are dynamically added to the dom, then use the following construct:

$(document).on('click', '.apply_button, .canget', function(e) {
  // код
  });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question