Run Javascript on select change

I have a richeditor and select field in a form. The select field is a relationship which Model also contains a richeditor field. I now want the richeditor field from the form to be auto-filled with the currently selected relation's richeditor content. Is there a way to trigger javascript and pass the relationship's model data on select change? My current Idea is to add a livewire component to the schema function of the form which only contains javascript and then adding a changed listener to the select input. I however don't know how to fetch the relationship data using this approach. Does anyone have a idea to do this/inject javascript into a form page as well as getting the Model data of the selected relationship?
2 Replies
Patrick Boivin
Patrick Boivin11mo ago
Not sure if I'm missing something but I don't think you need the custom JS. Have you tried something like this on the Select field?
->reactive()
->afterStateUpdated(function ($state, $set) {
$content = // use $state to get editor content...

$set('my_rich_text', $content);
})
->reactive()
->afterStateUpdated(function ($state, $set) {
$content = // use $state to get editor content...

$set('my_rich_text', $content);
})
skyracer2012
skyracer201211mo ago
Thanks! I actually tried afterStateUpdated out but ruled it out because it only worked when clicking save. Didnt think about ->live() thanks!