Edit: Not sure if you meant jquery mobile or a mobile jquery site, but if you are indeed using jquery mobile, see below:
A few things since you note above that you're using jquery mobile:
Use $(document).on('pageinit'), not $(document).ready():
The first thing you learn in jQuery is to call code inside the
$(document).ready() function so everything will execute as soon as the
DOM is loaded. However, in jQuery Mobile, Ajax is used to load the
contents of each page into the DOM as you navigate, and the DOM ready
handler only executes for the first page. To execute code whenever a
new page is loaded and created, you can bind to the pageinit event.
This event is explained in detail at the bottom of this page.
Also, you may need to insert your .focus
code inside an event, such as the pagecreate
event:
Triggered when the page has been created in the DOM (via ajax or
other) but before all widgets have had an opportunity to enhance the
contained markup. This event is most useful for user's wishing to
create their own custom widgets for child markup enhancement as the
jquery mobile widgets do.
See more events on the documentation here.
If you're just using regular jQuery, then you may need to wrap your code within the $(document).ready(function(){ ... });
. This helps ensure that the DOM is loaded and allows your jQuery selector $("#txt")
to find the element on the page. If the DOM hasn't loaded yet when your JavaScript is ran, the input
will not be found.