When I render an html.erb partial the div shows up on the screen but the javascript is not firing, how can I get the javascript to run?
Controller render
render partial: 'success', status: 200
Partial _success.html.erb
<div class="js-hide notification is-primary mb-16">
Success!
</div>
<script language="javascript" type="text/javascript">
document.addEventListener("turbolinks:load", () => {
alert('this should run');
setTimeout(function() {
$(".js-hide").hide();
}, 5000);
})
</script>
Stimulus Form Controller
import { Controller } from 'stimulus';
export default class extends Controller {
static targets = ['input', 'status', 'form'];
success(event) {
const [data, status, xhr] = event.detail;
this.statusTarget.innerHTML = xhr.response;
this.inputTarget.value = '';
this.formTarget.remove()
}
error(event) {
const [data, status, xhr] = event.detail;
this.statusTarget.innerHTML = xhr.response;
this.inputTarget.value = '';
}
}
Input form
<div class="mb-24" data-controller="form">
<p data-target="form.status"></p>
<%= form_with(model: email_follow, url: form_path(event),
data: { action: 'ajax:success->form#success
ajax:error->form#error', target: 'form.form' }
) do |form| %>
<div class="field has-addons">
<div class="control">
<%= form.text_field :name, class: 'input', data: { target: 'form.input' } %>
</div>
<div class="control">
<%= form.submit 'Submit', class: 'button is-info' %>
</div>
</div>
<% end %>
</div>
RESULT: Ended up adding the js code into my stimulus controller