mikemaccana
mikemaccana

Reputation: 123440

How can I focus on a selectize select box?

Focusing on a select box (that has selectize enabled) does not focus on the selectized input box:

$('.someclass select').focus();

Focusing on selectize's own inout box doesn't seem to work either:

$('.someclass input').focus();

The Selectize docs mentions focus but that doesn't seem to work either. See this jsfiddle:

var selectized = $('#selectize').selectize();
selectized.focus();

I would expect the carat | to be ready and typing to immediately go into the box.

How can I focus on a selectize select box from JavaScript, so a user can type into it?

Upvotes: 13

Views: 11097

Answers (5)

Matthew Jordan
Matthew Jordan

Reputation: 1

I found a easy way:

$("#selectid-selectized").focus(
  function () { 
    $("#selectid")[0].selectize.clear(); 
})

Upvotes: 0

Anca Sârbu
Anca Sârbu

Reputation: 1

For me $('.someclass input').focus() did the job. Firstly you choose your selectize element $('.someclass'), afterwards you choose to select only the input for that element $('.someclass input') and finally you trigger the focus() function on the element.

Upvotes: 0

fatpanther
fatpanther

Reputation: 115

$('.someclass input') returns a jquery object (wrapped set - see What does jquery $ actually return?). You don't want the wrapped set, you want the first element in the set that matches the selector.

Try $('.someclass input')[0].selectize.focus(); instead.

Upvotes: 2

amdramdas
amdramdas

Reputation: 513

You should use:

selectized[0].selectize.focus();

Upvotes: 25

James Donnelly
James Donnelly

Reputation: 128776

One way which appears to work is to call click() instead of focus():

$('.someclass input').click();

Upvotes: 7

Related Questions