Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 649
mikemaccana Avatar asked Dec 19 '14 12:12

mikemaccana


3 Answers

You should use:

selectized[0].selectize.focus();
like image 63
amdramdas Avatar answered Oct 19 '22 20:10

amdramdas


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

$('.someclass input').click();
like image 33
James Donnelly Avatar answered Oct 19 '22 19:10

James Donnelly


$('.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.

like image 2
fatpanther Avatar answered Oct 19 '22 19:10

fatpanther