'Cannot access input element in partial with Stimulus (Rails)
I have a normal Rails one to many (one project has many tasks) application that works perfectly fine. What doesn't work is accessing the value of an input field that's rendered in a partial with a Stimulus method that is not connect().
So I have a partial for tasks in which I have an input field called title.
When I do
connect() {
console.log(this.titleTarget.value)
}
I get the value of the title (which is obviously empty at the beginning), but when I try to access the value in another method via button click, it fails.
So I have a button in the parent form that triggers the method 'copy' when clicked. This also works perfectly when the method looks like this:
copy() {
console.log('clicked')
}
But it fails and throws an error message when I try to access the input field's value:
copy() {
console.log(this.titleTarget.value)
}
This gives me this:
stimulus.js:2855 Uncaught Error: Missing target element "title" for "project" controller
Everything is obviously connected correctly since I can get the input's value and click on the button to console log something, but they do not work together and I don't understand why.
Also, I'm honestly just playing around with Stimulus, so there is no practical reason behind any of it, I'm just confused.
Thanks!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
