The documentation shows data-controllername-valuename-value="thevalue"
only on the html element defining the controller.
What I’m trying to do is replace a data-valuename="thevalue"
on a number of targets and access each when processing the given target.
It doesn’t seem to be possible though… does this mean, I should stay with the dataset.valuename
access for targets?
With an interaction defined like so
/ apologies - long lines
div data-controller="dependent-form-items"
.ml__feedback-field
= f.check_box :negative_experience_checkbox, class: 'ml__feedback-checkbox', data: { action: "click->dependent-form-items#toggleCheck"}
= f.label :negative_experience_checkbox, "I had a negative experience", class: 'ml__feedback-label'
.field.field--indented data-dependent-form-items-target="dependent" data-show-if="1" data-dependent-form-items-show-if-value="1"
= f.label :negative_experience, "Please provide details", class: "label"
= f.text_area :negative_experience, size: "60x3"
and controller
...
static values = { showIf: String }
...
toggleCheck() {
this.dependentTargets.forEach((el) => {
// possible to access el.dataset.showIf
// not possible to access el.showIfValue
if ( el.dataset.showIf.split(' ').includes(event.currentTarget.value) && event.currentTarget.checked ) {
el.classList.remove('hidden')
} else {
el.classList.add('hidden')
}
})
} // toggleCheck
Am I missing something or is this not intended to work on targets?