What is the best way to bind click events?
Hello. I'm an 'experienced' HTML5/CSS3/JavaScript Front-End developer and this is my first time using a framework that employs a dynamic DOM instead of loading pages, and I've come to the question:
What is the best way, considering how jQ Mobile handles its dynamic DOM, to bind click events?
So far I have tried 3 methods that work, but I do not know which is the preferred one:
- <div data-role="page">
- .....
- .....
- <script type="text/javascript">
- $("#mydiv").click(function() { ... });
- </script>
- </div>
- $(":mobile-pagecontainer").on( "pagecontainerchange", function( event, ui ) {
- if( $(":mobile-pagecontainer").activepage().attr("id") == "myPage) {
- //Unbind any previous events just in case this element was already in the DOM.
- $("#mydiv").unbind();
- $("#mydiv").click(function() { ... });
- }
- } );
- <div data-role="page">
- <div id="mydiv" onclick="myAction()">
- ....
- </div>
- </div>
Which one is the best way, or is there a better way than the ones I have used?