Skip to content

How to get the text-field's input element to work with events? #5395

Answered by asyncLiz
alexmeier-19 asked this question in Q&A
Discussion options

You must be logged in to vote

Ok, I guess I found a way. If I get the <md-outlined-text-field id="12345"...></..> element by id in JS I'll get the MdOutlinedTextField Object with the whole API already, won't I?

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.

<md-outlined-text-field id="12345"></md-outlined-text-field>

<script type="module">
  const textField = document.getElementById('12345');
  textField.addEventListener('change', () => {
    textField.setCustomValidity(getErrorMessageOrEmptyString(textField.value));
    textField.reportValidity();
  });

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@alexmeier-19
Comment options

@alexmeier-19
Comment options

@alexmeier-19
Comment options

@asyncLiz
Comment options

Answer selected by alexmeier-19
@alexmeier-19
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants