Skip to content
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

connect() not work when change with javascript #554

Closed
karaka200 opened this issue Jun 30, 2022 · 3 comments
Closed

connect() not work when change with javascript #554

karaka200 opened this issue Jun 30, 2022 · 3 comments

Comments

@karaka200
Copy link

Hi. I create a stimulus controller and use on a div element.
Now when change innerHTML of this div element with click on a button, text change but color not change to red and connect() not work.

<div data-controller="test">
    <span>This text color change to red</span>
</div>
<button type="button" data-action="test#changeColor">click</button>

test_controller.js

...
connect() {
    this.element.style.color = "red";
}
changeColor(event) {
    this.element.innerHTML = "<span>HelloWorld!</span>"
}
...

@adrienpoly
Copy link
Member

actions are scoped inside the controller node

This will work

<div data-controller="test">
    <span>This text color change to red</span>
   
    <button type="button" data-action="test#changeColor">click</button>
</div>

if you need to have your button outside of this element then you probably need to define a target

@marcoroth
Copy link
Member

Hey @karaka200

The element/button containing the the data-action attribute needs to be under the element which has the data-controller on it.

@karaka200
Copy link
Author

@marcoroth @adrienpoly I'm sorry you right but in this code still not work:

<div data-controller="test">
    <span>This text color change to red</span>
   
    <button type="button" data-action="test#changeColor">click</button>
</div>

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

No branches or pull requests

3 participants