0

Possible Duplicate:
iPhone Trigger keyboard in input field (HTML / Javascript)

I am working on jquery mobile site. I want to give focus to text field using javascript or jquery so it will popup keyboard. I have tried .focus(), .select() and .trigger('focus') but it's not working. --HTML--

<input id='txt' type='search' value='' />

--JQUERY--

$('#txt').focus();  

$('#txt').select();

$('#txt').focus();
Community
  • 1
  • 1
Dipen Dedania
  • 1,452
  • 1
  • 21
  • 37
  • 1
    Take a look at http://api.jquery.com/focus/ – İsmet Alkan Jan 10 '13 at 15:30
  • Can't reproduce: http://jsfiddle.net/3aGbr/ – Quentin Jan 10 '13 at 15:31
  • 2
    Where do you run this code, in the page head? Try moving it to the bottom of the page, just before you close the body tag, or put it in a DOM-ready callback. It might be that the code is run before the element actually is in the DOM. – Christofer Eliasson Jan 10 '13 at 15:31
  • which mobile device are you trying this on? – vortextangent Jan 10 '13 at 15:33
  • 1
    possible duplicate of [iPhone Trigger keyboard in input field (HTML / Javascript)](http://stackoverflow.com/questions/5858545/iphone-trigger-keyboard-in-input-field-html-javascript) or http://stackoverflow.com/questions/4609765/manually-triggering-the-iphone-ipad-ipod-keyboard-from-javascript There may not be a solution to this. – Kevin B Jan 10 '13 at 15:34
  • try this document.getElementById('txt').focus(); – laxonline Jan 10 '13 at 15:35
  • You should look through your code and make sure you're not doing a .blur() anywhere else. It would be helpful if you could add more of your source code so we could find something like that. – Matthew Davis Jan 10 '13 at 15:40
  • Can you re-word your question? `.focus()` IS working, it just isn't displaying the iphone's keyboard. – Kevin B Jan 10 '13 at 15:44
  • ipad's and iphone's webkit is the new IE6 – kidwon Jan 10 '13 at 15:54
  • If you edit the question can you please leave the "duplicate link" text in place. – ChrisF Feb 04 '13 at 16:56

2 Answers2

2

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.

Chase
  • 29,019
  • 1
  • 49
  • 48
  • Thanks for the reply...I'm using jquery mobile.And my code is in pageshow event so the .focus() will execute every time. – Dipen Dedania Jan 11 '13 at 04:19
  • Is there any way you can update your question with the `pageshow` function that contains your `focus` call? That way I can take a look and see if I notice anything. Thanks – Chase Jan 11 '13 at 13:28
  • I found that it's actually impossible to focus in textbox using programming. it's usability issue.. – Dipen Dedania May 04 '13 at 14:38
0

It should be working but since it's not, I would try something not so normal like .click(). Have you tried it? It should work.

rafaelbiten
  • 6,074
  • 2
  • 31
  • 36