In a dockerized rail 6.1.7.2 application, I'm not able to have my custom javascript file working.
Its name corresponds to the view file where I want it to be loaded.
I can see that the JS asset is successfully transmitted in the network panel of the web browser when I hit this page, and when I open it, I can also see that my custom JS snippet is actually present at the very bottom of that .js file, underline in green in this screenshot:
But whatever I do with the checkbox on the web page, the div is always displayed and never disappears.
And if I test those snippets on JSFiddle, it works as expected: box ticked: the div is displayed, unticked: the div is hidden.
# cat app/assets/javascripts/custom.js
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
let checkbox = document.getElementById('show');
let box = document.getElementById('target');
box.style.visibility = 'hidden';
checkbox.addEventListener('click', function handleClick() {
if (checkbox.checked) {
box.style.visibility = 'visible';
} else {
box.style.visibility = 'hidden';
}
});
# cat app/views/custom/index.html.erb
<h2 class="title">Title</h2>
<table>
<tr>
<td>
<input type="checkbox" id="show"/>
</td>
<td>
<p>Tick me to display the target div!</p>
</td>
</tr>
</table>
<div id="target">
<h4 class="title">Foo</h4>
</div>
What could be wrong and how could I fix this strange behavior?
I cannot get it to work both in development and production.
There is no particular error or warning in the app/log/{production|development}.log files.
Edit
Adding <%= javascript_include_tag "custom.js", defer: true %>
to the HTML template doesn't work either. But the custom JS code is now loaded twice in the DOM:
With the corresponding behavior of the web page (without | with the checkbox ticked):
` or just add a `defer` prop in the script tag
– Joshua Mar 01 '23 at 02:40