How to get the text-field's input element to work with events? #5395
-
I'm quite new to material-web, but I find it already easier to use than material-components-web. I need to implement some custom complex validation and I wonder how can I get the text-field's input element, for example, to add event listeners to it in a convenient way? In mdc there was something like someMdcTextInput.listen('keydown', ...). I can find a private method getInput(), but that's it... |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
The text field works similarly to the platform's Text fields use constraint validation that you may want to familiarize yourself to check out. There's a lot of useful properties and methods like An example listener that checks a text field's value when it changes and reports the error when it's invalid: mdTextField.addEventListener('change', () => {
if (isUsernameAvailable(mwcTextField.value)) {
mdTextField.setCustomValidity('');
} else {
mdTextField.setCustomValidity('Username is unavailable.');
}
mdTextField.reportValidity();
}); You can also use built-in validation attributes like the |
Beta Was this translation helpful? Give feedback.
Yes! You do not need to create or attach anything. When the html element is rendered on the page, the custom element's javascript automatically attaches, whether it's loaded before or after the html is rendered.