Skip to content

onchange event doesn't fire #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dfoverdx opened this issue Mar 25, 2023 · 5 comments
Closed

onchange event doesn't fire #28

dfoverdx opened this issue Mar 25, 2023 · 5 comments
Assignees

Comments

@dfoverdx
Copy link

As near as I can tell, onchange doesn't ever fire.

<code-input lang="js" onchange="console.log(this.value)"></code-input>

The console.log never occurs. I stepped through the code and found one issue (line 71), but even when I fixed it, I didn't get any change events. AFAIK, changing the value via javascript doesn't trigger change events on any element, so you'd need to manually dispatch them.

@dfoverdx
Copy link
Author

Never mind. It does seem to be triggering static text values, but not when you use .addEventListener to the <code-input> element. My workaround (in React) was to use useRef() and useEffect() to attach the eventListener to the textarea's change event.

@WebCoder49 WebCoder49 self-assigned this Mar 30, 2023
@WebCoder49
Copy link
Owner

I will still try to implement this ASAP so others can use it intuitively. Thank you for letting me know about the issue.

@WebCoder49
Copy link
Owner

The pull request above should fix this.

@arohman84
Copy link

@WebCoder49 how to use addEventListener ? try using below but return error codeInput.CodeInput.addEventListener is not a function

codeInput.CodeInput.addEventListener('change', (val) => console.log(val))

@WebCoder49
Copy link
Owner

  1. @arohman84 I think you're running the addEventListener function on the codeInput.CodeInput class rather than a specific element. What you need to do is get a specific code-input element in JavaScript (with document.querySelector with a CSS selector, for example) then run (with that code-input element in the myCodeInputElement variable) myCodeInputElement.addEventListener('change', (evt) => console.log(myCodeInputElement)).
  2. You'll see that I changed your code slightly above. This is because the "val" parameter you are using will actually contain the change event object.
  3. If you keep having problems, please make sure you are using version 2.1.0 of the library.

I've put this all in a small demo here. If you have any questions / suggestions for this library, please feel free to let me know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants