Introduction
First of all, a mobileinit event should not be used to bind an event. Although it can be used as mobileinit was not created for this purpose. It was created to automatically initialize the jQuery Mobile parameter, so it should not be used to bind an event.
The right way is to use the correct page events, such as pageinit . For more information on page events, check out my other answer, which covers the various events of the jQuery Mobile page and how they differ from the regular jQuery document paradigm: jQuery Mobile: the document is ready for the event page .
Do not let me answer this question. Events such as clicks can be connected in several different ways. Let's look at the examples you used:
Different ways to bind events to jQuery
First example
$(document).on('click', '#one1', function(e){ console.log('firing'); });
This first example is something new that was first used with the now obsolete live method. Basically, this is an event delegation mechanism that allows you to associate event handlers not only with all existing instances of a given node type, but also with any future instances of this node type (by type "I" means a set of DOM nodes mapped by this jQuery selector). Here I want to say that during an event related to the fact that this element should not exist in the DOM, this method basically works by attaching event handlers to the document itself and then responds to all events that bubble through the DOM. Therefore, it does not matter if element # one1 exists or not during event binding. You can create it dynamically later, and it will still work.
Second example
$('#one1').on("click", function() { console.log('not firing'); });
This is an old way to bind an event. This requires the event to exist in the DOM before the event can be bound. In your case, you tried to bind this click event to an element that did not exist in the DOM at this point in time. It doesnโt matter, it was loaded after the binding process.
Working example
JsFiddle example: http://jsfiddle.net/Gajotres/QmNsa/
Take a look at this example. There you will see 5 different ways to bind the click event in jQuery Mobile:
- The 2 click event is bound in HEAD before the page is initialized in the DOM
- Two-click events are linked in HEAD in the pagebeforeshow event, basically this is also delegation of binding, because the event is bound when the page will be displayed and is already inside the DOM
- The 1 click event is associated with BODY after the entire content of the page. Since all content is loaded inside the DOM, at this point this event will work.
HTML:
<!DOCTYPE html> <html> <head> <title>jQM Complex Demo</title> <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" /> <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script> <script> $(document).on('click', '#one1', function(e){ </script> </head> <body> <div data-role="page" id="index"> <div data-theme="b" data-role="header"> <h1>Index page</h1> </div> <div data-role="content"> <a href="#" id="one1" data-role="button">[one]</a> </div> </div> <script> $('#one1').on("click", function() { </script> </body> </html>
Conclusion
- Do not use the mobileinit event to bind the event, it will be fired before the page loads in the DOM, and only events related to delegation will work.
- Link your events to the correct jQuery events mobile page.
Useful links on this topic: