lekoala/bootstrap5-tags

Add Tag via jquery

CBSTV opened this issue · 2 comments

CBSTV commented

Hey,

This might be a stupid question but am i able to add a new tag to the element via jquery?

I have buttons that render with dynamic content depending on who is on the webpage. Ideally clicking the buttons would add the tag to the element.

Thanks
C

@CBSTV hey there, you don't need jquery for that, just us regular js. if you want to bind listeners on your buttons with jquery that's fine too :) (but again, not really necessary)

get the instance, and use public methods
there is a add item just as if you clicked on a selection

const inst = Tags.getInstance(el);
inst.addItem(v);

you can also dynamically adjust the options as needed, either through server side or even just manipulating the available option elements and refresh the list

let me know if you have other questions. if that doesn't work, make a jsfiddle or a codepen and i'll have a look

CBSTV commented

A lot easier than i thought! Thank you so much, works exactly as expected.