Activate the 'add' option by default in Tom Select

1.2k Views Asked by At

I've enabled the "add" option on a Tom Select, which appears at the top of the dropdown when typing in a filter term. Eg:

new TomSelect(target, {
  create: true
});

However, the closest matching option is selected (see pictured). I'd like the "add" option to be selected (highlighted/activated) by default. Is this possible?

tom-select with second option highlighted

I've figured out how to listen to the 'type' event, which happens each time you type into the filter input, but am not sure how to select the "add" option.

2

There are 2 best solutions below

7
JohnLBevan On BEST ANSWER

Please try setting addPrecedence to true.

new TomSelect(target, {
  create: true,
  addPrecedence: true
});

Related documentation.

1
Dandelione Code On

Otherwise, if you want to disable "add" in the dropdown, setting addPrecedence to "false" and add the following code to the option.

:options="{
   create: false,
   addPrecedence: false,
}"

For an explanation of addPrecedence, see https://tom-select.js.org/docs/