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.

Select2 field .value property doesn't change inside .onchange function

See original GitHub issue

I’m having this very specific issue of which I have not been able to find a solution. I have a Select2 field on which after selecting a specific company it will auto fill with it’s corresponding country based on values fetch from an API on a .onchange function triggered on a Select2 field from which you select the corresponding company to fetch from.

All other auto-filled fields apart from this one, get populated with it’s corresponding value, except for the Select2 field for the country value that only gets selected but not displayed has chosen.

However, if I try to change the .value property outside the .onchange function, the field does change completely and display the currently selected country on the list.

I already tried executing the function either by changing the .value property with vanilla JavaScript and using the .val method with JQuery following the recommendations on the Select2 documentation, but the same result occurs, here I attach the code and some screenshots.

const countryField = document.querySelector(
    'select[name=' + country + ']'
)

selectField.onchange=function() {
    const apiUrl = new URL(url)
    const params = {q: $(this).val()}
    apiUrl.search = new URLSearchParams(params).toString();
    fetch(apiUrl)
        .then((response) => response.json())
        .then(function(data) {
            const fieldsToUpdate = [
                {field:addressLine1Field,value:data.address_line1},
                {field:addressLine2Field,value:data.address_line2},
                {field:cityDistrictField,value:data.city_district},
                {field:stateProvinceField,value:data.state_province},
                {field:postalCodeField,value:data.postal_code},
                {field:countryField,value:data.country},
            ]
            for (const { field,value } of fieldsToUpdate) {
                if (!!field) {
                    field.value = ''
                    if (!!value) {
                        field.value = value
                    }
                }
            }
        })
        .catch(function(error) {
           console.log(error);
        });
}

Country is only selected on list but not display has selected.

While changing the property outside the function results in this:

const countryField = document.querySelector(
    'select[name=' + country + ']'
)
countryField.value = 'Canada'

Country is selected on the list and displayed has selected.

So far I’m still not sure what makes the property change works different on the DOM while executing it inside the .onchange method. If anyone out there might have any idea what it could be, any help would be appreciated. Thank you all.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
kevin-browncommented, Jan 8, 2021

Are you able to reproduce this issue in a jsbin (doesn’t need to be a fancy example) so we can use it for an isolated test case? That should make it easier to trace the issue, since it sounds specific to how the browser binds to this event.

0reactions
stale[bot]commented, Jun 2, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Select2 field .value property doesn't change inside .onchange ...
I have a Select2 field on which after selecting a specific company it will auto fill with it's corresponding country based on values...
Read more >
Methods | Select2 - The jQuery replacement for select boxes
Select2 has several built-in methods that allow programmatic control of the component. Opening the dropdown. Methods handled directly by Select2 can be invoked ......
Read more >
Common problems - The jQuery replacement for select boxes
Select2 does not function properly when I use it inside a Bootstrap modal. ... This issue occurs because Bootstrap modals tend to steal...
Read more >
Events | Select2 - The jQuery replacement for select boxes
Select2 will trigger a few different events when different actions are taken using the component, allowing you to add custom hooks and perform...
Read more >
Search | Select2 - The jQuery replacement for select boxes
When users filter down the results by entering search terms into the search box, Select2 uses an internal "matcher" to match search terms...
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