I am trying to write a bit of code using JQuery's .hover
method but have found that it works when written in the main html file index.html
but does not when written in a javascript file referenced as <script language="JavaScript" type="text/javascript" src="main.js"></script>
. Currently I am trying to get my head around the hover method by applying it to the button tag. It successfully executes the script written in the html file but does not execute the script in main.js
I am not receiving any errors in console and main.js
is being referenced correctly as there is other code in that file that is running successfully
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Sudoku Solver</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="style.css">
<script language="JavaScript" type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script language="JavaScript" type="text/javascript" src="main.js"></script>
<script language="JavaScript" type="text/javascript" src="sudokuSolver.js"></script>
</head>
<body>
<button class='solveSudoku' style="margin-right: 30px">Solve!</button><button class='clearSudoku'>Clear</button>
<div id='container'></div>
<script>
$("button").hover(
function () {
alert("Test")
}, function () {}
);
</script>
</body>
</html>
main.js
function createGrid() {
for (var rows = 0; rows < 9; rows++) {
for (var columns = 0; columns < 9; columns++) {
cell = "<div class='grid' "
if (rows%3==2) {
cell += "style='box-shadow: 0 1px 0 #000, "
} else {
cell += "style='box-shadow: 0 1px 0 rgb(150, 150, 150), "
}
if (rows%3==0) {
cell += "0 -1px 0 #000, "
} else {
cell += "0 -1px 0 rgb(150, 150, 150), "
}
if (columns%3==2) {
cell += "1px 0 0 #000, "
} else {
cell += "1px 0 0 rgb(150, 150, 150), "
}
if (columns%3==0) {
cell += "-1px 0 0 #000;'></div>"
} else {
cell += "-1px 0 0 rgb(150, 150, 150);'></div>"
}
$("#container").append(cell);
}
}
$(".grid").width(90);
$(".grid").height(90);
}
$("button").hover(
function () {},
function () {
alert("Test2")
}
);
$( document ).ready(function() {
createGrid()
});