pryley / float-labels.js

A zero-dependency plugin that applies the float label pattern to a form.
https://pryley.github.io/float-labels.js/
MIT License
88 stars 22 forks source link

Setting field value through javascript doesn't show the label #19

Closed schellenbergk closed 6 years ago

schellenbergk commented 6 years ago

Hello, great plugin by the way....

When I set the field value using javascript the label does not show up. I've tried triggering change event too but no success.

input.value = val;
input.dispatchEvent(new Event('change'));

However I was able to use this workaround but see if there is a way of updating float-labels so workaround is not needed... Thanks...

input.value = val;
el.parentNode.classList.add('fl-is-active');
pryley commented 6 years ago

Try this:

input.value = val;
input.dispatchEvent(new Event('input'));