Obromios Obromios - 6 months ago 10
Javascript Question

jQuery DOM event listener failed with "$ is not defined"

Following the advice here, I have the following inline code in a Ruby on Rails app:

document.addEventListener("DOMContentLoaded", function(event) {
$("select#graph_line_duration").change(function(){
$("form").submit();
});
$("input#graph_duration").hide();
});


I am getting an error
$ is not defined
. However, if I click on a link to go to the page, then the code works fine, so my
jquery
assets are loading, given sufficient time.

Answer

try this:

document.addEventListener("load", function(event) {

https://developer.mozilla.org/en-US/docs/Web/Events/DOMContentLoaded

The DOMContentLoaded event is fired when the document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading (the load event can be used to detect a fully-loaded page).