Binding More Than One Dynamically Added Elements

Benjamin Anderson

I'm using jQuery Mobile and I need to bind some tap events to dynamically added content. It seems that the way I am doing it, my code only runs the last element I attach a tap event to inside my code. For example, this code will not alert "Hi" when I click on element A but it will alert "Bye" when I click on element B.

I'm sure this is a simple mistake but I've been banging my head against the desk trying to figure it out.

$(document).off("tap").on("tap", "#A", function() {
    alert('Hi');
});

$(document).off("tap").on("tap", "#B", function () {
    alert('Bye');
}); 
PSL

It is because you are attaching the event to the document and then unbinding it in the next line. So your binding on A is lost when you turn off tap on document and bind again on document for b.

Try:

$(document).off("tap");
$(document).on("tap", "#A", function() {
    alert('Hi');
});

$(document).on("tap", "#B", function () {
    alert('Bye');
}); 

Also you can chain it though as well just to avoid creating jquery object again and again.

$(document).off("tap")
  .on("tap", "#A", function() {
    alert('Hi');
}).on("tap", "#B", function () {
    alert('Bye');
}); 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Binding dynamically added elements in jQuery mobile

From Dev

Data-binding with dynamically added elements

From Dev

qooxdoo: Binding more than one proberty to a label

From Dev

Avoid repeated binding of Events for dynamically added DOM elements?

From Dev

jquery dynamically added divs firing a function more than once

From Dev

Replacing more than one elements with replace function

From Dev

CSS specificity for elements with more than one class

From Dev

Android: makeSceneTransition for more than one shared elements

From Dev

ddCollapse not working for more than one elements

From Dev

Replacing more than one elements with replace function

From Dev

Can a single fragment instance be added simultaneously to more than one container?

From Dev

JPanel disappears when added to more than one other JPanel

From Dev

Printf not aligned when more than one extra digit is added

From Dev

knockout options binding controls for more than one model

From Dev

How to add more than one WHERE clause dynamically in php

From Dev

How to add more than one WHERE clause dynamically in php

From Dev

remove div elements if the div length is more than one using jquery?

From Dev

get the index of the string elements which are common to more than one lists

From Dev

Find Codeception elements with more than one CSS class

From Dev

jQuery to PHP: Add a selector class to children elements if more than one

From Dev

How to count the elements in array which are more than one (php)

From Dev

remove div elements if the div length is more than one using jquery?

From Dev

get the index of the string elements which are common to more than one lists

From Dev

push more than one elements at same index in array

From Dev

Why does my linked list only contain one node even though I added more than one?

From Dev

Jquery css() on dynamically added elements

From Dev

Call function on dynamically added elements

From Dev

Working with dynamically added elements in jquery

From Dev

Modify dynamically added elements in FlowLayoutPanel

Related Related

  1. 1

    Binding dynamically added elements in jQuery mobile

  2. 2

    Data-binding with dynamically added elements

  3. 3

    qooxdoo: Binding more than one proberty to a label

  4. 4

    Avoid repeated binding of Events for dynamically added DOM elements?

  5. 5

    jquery dynamically added divs firing a function more than once

  6. 6

    Replacing more than one elements with replace function

  7. 7

    CSS specificity for elements with more than one class

  8. 8

    Android: makeSceneTransition for more than one shared elements

  9. 9

    ddCollapse not working for more than one elements

  10. 10

    Replacing more than one elements with replace function

  11. 11

    Can a single fragment instance be added simultaneously to more than one container?

  12. 12

    JPanel disappears when added to more than one other JPanel

  13. 13

    Printf not aligned when more than one extra digit is added

  14. 14

    knockout options binding controls for more than one model

  15. 15

    How to add more than one WHERE clause dynamically in php

  16. 16

    How to add more than one WHERE clause dynamically in php

  17. 17

    remove div elements if the div length is more than one using jquery?

  18. 18

    get the index of the string elements which are common to more than one lists

  19. 19

    Find Codeception elements with more than one CSS class

  20. 20

    jQuery to PHP: Add a selector class to children elements if more than one

  21. 21

    How to count the elements in array which are more than one (php)

  22. 22

    remove div elements if the div length is more than one using jquery?

  23. 23

    get the index of the string elements which are common to more than one lists

  24. 24

    push more than one elements at same index in array

  25. 25

    Why does my linked list only contain one node even though I added more than one?

  26. 26

    Jquery css() on dynamically added elements

  27. 27

    Call function on dynamically added elements

  28. 28

    Working with dynamically added elements in jquery

  29. 29

    Modify dynamically added elements in FlowLayoutPanel

HotTag

Archive