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.

Other ways to trigger dropdown's onChange? It does not fire with 'set selected'

See original GitHub issue

I have a simple setup like

$element.dropdown({
  fullTextSearch: true,
  onChange: function() {}
})

and then have an ajax at the end to pre-fill it

$.ajax({
 // settings here
})
.done(
  // iterate results and made <div class="item" data-value=value here>text here</div> with it
  // and added it using .html()
  
  $element
    .dropdown('set selected', 'defaultValue')
)

onChange does not fire when doing that. Tried putting it insde setTimeout and still not working.

BUT it does work when manually clicking or doing it from the console like $element.dropdown('set selected', 'otherValue')

I can see the defaultValue being selected but it’s just the event onChange not firing.

tried doing .find() and .trigger('change') on the hidden input but no luck. even triggering change from the dropdown itself.

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
awgvcommented, Feb 27, 2017

Hi @xxRockOnxx, when it’s done() and you populated it with options, try executing the $el.dropdown('refresh') before selecting the default value, and please let me know if it helped:

$element
  .dropdown('refresh')
  .dropdown('set selected', defaultValue)
1reaction
xxRockOnxxcommented, Feb 28, 2017

I think I found the cause of the problem in case other might experience the same issue as me.

There was a value attribute on the input itself. Might work if the value is not the same as the one you put in set selected. Doing set selected with the same value as the current one will not trigger a change.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JavaScript onchange event won't fire if select option is ...
What I'm trying to do is to get the select element onchange event to fire regardless where the option is being changed from....
Read more >
.change() | jQuery API Documentation
Attaches a change event to the select that gets the text for each selected option and writes them in the div. It then...
Read more >
HTMLElement: change event - Web APIs | MDN
The change event is fired for <input> , <select> , and <textarea> elements when the user modifies the element's value.
Read more >
Web dropdown list's onchange event cannot be triggered ...
I have dropdown list in my website. and an onchange event is binding to that dropdown list. I can use the 'select value'...
Read more >
Bug(?): Dropdown OnChange behavior not as expected...
The OnChange property for dropdowns(with this collection as its items property or default) also set global variables, I do this so that screens ......
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