Hey, thanks for your reply. Here's some clarification (I hope).
I can't use JSfiddle, because this involves more than just javascript.
Imagine you have one page that's dynamically generated by the server based on the query string. Then you have a button on the page that will perform an action. You tie a click event handler to the button. Easy enough.
Now, you load the page again, with a different query string. The javascript ties the event handler to the button again. Again, easy enough.
Now let's move to using jQueryMobile. It loads the first page into the DOM. Ties the event handler to the button. Everything works dandy. Then you go to the same page with a different query string. jQueryMobile correctly determines that it's a new page, and loads it into the DOM without unloading the first page. This is behavior I like, and approve of and would like to keep.
However, when we try to attach the event handler now, we run into my problem. There are two options (I've tried) to attach the event handler.
- Use an id attribute of the button "#id". It works fine on the first page, but when we load the next page, we suddenly have two buttons with the same id. Problem! The event handler is tied to the first page's button a second time. This causes the event to be triggered twice for the first page's button (if we navigate back) and not at all for the second page's button.
- Use a non-unique attribute of the button "input[type=button]". It works fine on the first page, but when we load the second page, the button event handler is tied to ALL the buttons with that selector. So when we navigate back to the first page, the event is run twice. This is a problem.
So my question is, what is the best practice for making certain I only attach the event handler once to each button? Let me know if I need to make it any clearer.
Thank you for your help.