question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to get a handle for an initialized Choices element

See original GitHub issue

If I have initialized my Choices element in the following way:

new Choices(document.querySelector(#choices'));

Is there a way to access the initialized Choices element somewhere else in the code if I don’t have a reference to the original value? As far as I know, using new Choices always returns a new, uninitialized value.

The use case could be, for example, that I want to programmatically set the value (.setValue())

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:11
  • Comments:24

github_iconTop GitHub Comments

9reactions
msimchacommented, Feb 25, 2020

I found that you can set the value of an existing Choices instance that you cannot access but triggering the mousedown event on the item with the value for which you’re looking:

handleSelect (obj, value) { let selectFields = obj.closest('.choices').querySelectorAll('.choices__item--selectable') for (let i = 0; i < selectFields.length; i++) { if (selectFields[i].dataset.value === value) { console.warn(selectFields[i].dataset.value) selectFields[i].dispatchEvent(new Event('mousedown')) } } } (obj here is a select element)

4reactions
aleehedlcommented, Mar 29, 2018

Hi,

The point was exactly that I don’t have the option to assign it to a variable.

On Wed, 28 Mar 2018, 17:31 Josh Johnson, notifications@github.com wrote:

Hey,

All you need to do is assign it to a variable e.g.

const instance = new Choices(document.querySelector(#choices’));instance.setValue(…);

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jshjohnson/Choices/issues/376#issuecomment-376907996, or mute the thread https://github.com/notifications/unsubscribe-auth/AANfNycNy85QKI_GV30YXvXuVy0-GlBEks5ti569gaJpZM4S-tQH .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handling Events
Most events are called on a specific DOM element and then propagate to that element's ancestors, allowing handlers associated with those elements to...
Read more >
Choices JS Re-Initialize is not working for dynamic Row add
I've faced similar kind of issue first i make each element with unique class and then call it from JS code, My code...
Read more >
Add Choices.js for tags multi-select | Part 25 - YouTube
In this lesson, we will add the choices.js for tags multi-selectStarter Template: GitHub Link: ...
Read more >
EventTarget.addEventListener() - Web APIs | MDN
It works on any event target, not just HTML or SVG elements. ... when both elements have registered a handle for that event....
Read more >
7 Tips to Handle undefined in JavaScript
an uninitialized variable number; a non-existing object property movie.year; or a non-existing array element movies[3]. are ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found