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.

Restore select to initial value

See original GitHub issue

hey,

I checked the live preview in the documentation, the button to reset the form (button type="reset") causes select not to keep the default value, select is empty

this feature works fine in version 9.0.1, the bug has appeared since version 9.1.0

how to reproduce the issue:

Zrzut ekranu 2022-03-21 o 11 26 45

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:10

github_iconTop GitHub Comments

2reactions
masioramacommented, Sep 9, 2022

As a temporary workaround in my application I managed to intercept reset event on the form and then repopulate all the options by hand:

document.getElementById('formId').select.addEventListener('reset', (ev) => {
    choices.setChoices(async () => {
        return this.options.map(({ value, label }) => ({
            value,
            label,
            selected: false,
        }));
    });
})

This should be unnecessary but at least my app does work. I hope this will be fixed at some point.

1reaction
Soglcommented, Dec 18, 2022

@masiorama I tried your code and saw that the event is not triggered:


const choicesTagsObj = new Choices('#choices-tags', {
            maxItemCount: 4,
            removeItemButton: true,
            allowHTML: false,
            position: 'bottom',
            itemSelectText: 'Push',
            maxItemText: (maxItemCount) => {
                return `Only ${maxItemCount} can be selected`;
            },
        });

document.getElementById('choices-tags').addEventListener('reset', (ev) => {
     console.log('reset');
})

what’s the problem?

After some minutes

Some points:

  1. Reset event works only for a form, not for a select element.
  2. Choices deletes options structure from a select, so you need to copy it into var before create new Choices element.

Working code:

let choicesTags = document.getElementById("choices-tags");
//copy full select node with values
let choicesTagsBefore = choicesTags.cloneNode(true);
//convert to array
choicesTagsBefore = Array.from(choicesTagsBefore.options); 

const choicesTagsObj = new Choices(choicesTags, {
    maxItemCount: 4,
    removeItemButton: true,
    allowHTML: false,
    position: 'bottom',
    itemSelectText: 'Push',
    maxItemText: (maxItemCount) => {
        return `Only ${maxItemCount} can be selected`;
    },
});

//fill again after form reset (Choices.js bug workaround)
document.getElementById('therapy-form').addEventListener('reset', (ev) => {
    // console.log('reset');
    choicesTagsObj.setChoices(async () => {
        return choicesTagsBefore.map(({ value, label }) => ({
            value,
            label,
            selected: false,
        }));
    });
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Reset select value to default - javascript - Stack Overflow
Reset select value to default · If you want to reset the whole form, just call the built-in JavaScript . · possible duplicate...
Read more >
How to reset selected value to default using jQuery
There are two approaches that are discussed below: Approach 1: First select the options using jQuery selectors then use prop() method to get ......
Read more >
Reset (Clear) DropDownList selection (selected value) using ...
The following HTML Markup consists of an HTML DropDownList (DropDown) control and a Button. When the Button is clicked, the Reset JavaScript function...
Read more >
How do I reset values in a select dropdown - CodeProject
i have a modal with this dropdownlist it is works fine, but when i close i would like to clear this dropdown and...
Read more >
<input type="reset"> - HTML: HyperText Markup Language
elements of type reset are rendered as buttons, with a default click event handler that resets all inputs in the form to their...
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