Marius Stănescu
Marius Stănescu

Reputation: 3761

Enable an input textbox then focus it - jQuery Mobile

I have an input textbox which is initially disabled.

<input type="text" name="count" id="count" value="0" disabled="true" data-theme="b" />

When I check a radio button i want to enable and focus the textbox, but when i click the radio button it enables the textbox and focuses some other textbox. I think it does not work because the textbox was initially disabled and you can not focus a disabled input.

$(":input[@name='radio']").live('change', function() { 
  $(":input[@name='count']").removeAttr("disabled").focus();
});

How do I enable the textbox and then focus it?

Upvotes: 3

Views: 5202

Answers (4)

Anish
Anish

Reputation: 3172

You have to do it on pageshow.

  $("#mainPage").on("pageshow", function (e) {
    $('#searchField').focus();
});

on

<body class="ui-mobile-viewport ui-overlay-c">
    <div data-role="page" data-add-back-btn="true" id="mainPage">
        <div data-role="header" data-theme='b'>
           <input type="text" id="searchField" placeholder="Search" />
        </div>
    </div>
</body

Upvotes: 0

Marius Stănescu
Marius Stănescu

Reputation: 3761

It appears it was something wrong with the selector ... although the text input box got enabled, it did not get focused, while I tried:

$(":input[@name='count']").removeAttr("disabled").focus();

But when i tried this, it worked:

$("#count").removeAttr("disabled").focus();

I can not explain why this is happening ...


Edit: It has something to the with the selector, because this works too:

$("input[name='count']").removeAttr("disabled").focus();

but i still can't understand why using the first selector, the textbox gets enabled, but not focused ...

Upvotes: 3

Kunal Vashist
Kunal Vashist

Reputation: 2471

It working fine for me

see

http://jsfiddle.net/kunalvashist/LFXd2/

Upvotes: 0

Hkachhia
Hkachhia

Reputation: 4539

Try below code

$('#count').textinput('enable').focus();

Upvotes: 1

Related Questions