multi-input icon indicating copy to clipboard operation
multi-input copied to clipboard

Pre-selecting options?

Open edent opened this issue 2 years ago • 1 comments

Great little library - just a quick question. I want to have some of my options already selected when the user loads the page.

Using <option value="dog" selected> doesn't seem to make any difference.

Is there any way to display some pre-selected items by default?

Thanks

edent avatar May 10 '23 09:05 edent

I found a nice way.

When you load your html or php you put all the values of your pre-selected options into a hidden input. Mine looks like this: <input type="hidden" id="articles" name="articles" value="CL-0300|CL-0300|CP-01-S|"> I separated my 3 pre-seleced options-values by a | to split them later by this special character.

Afterwards, you let javascript load all the divs right after the MultiInput is initialized (meaning after line 76):

// load pre-selected options let articles = document.getElementById('articles').value; let articlesArray = articles.split("|"); let i = 0; while (i < articlesArray.length - 1) { this._addItem(articlesArray[i]); i++; }

Works beautifully.

Matye91 avatar Feb 14 '24 11:02 Matye91