I am trying to make two empty <div> elements clickable through the use of HTML and Javascript alone. No jQuery.
I have this HTML document...
<!DOCTYPE html>
<html>
<head>
<title>whatever</title>
<link rel="stylesheet" href="css_squares" media="screen" >
<script src="lala.js"></script>
</head>
<body>
<h1>Clickable Squares</h1>
<div id="top_square" onclick="alert('Top square clicked')">
</div>
<div id="bottom_square">
</div>
</body>
</html>
and also have this simple CSS...
#top_square {
width: 50px;
height: 50px;
background-color: #01FF55;
}
#bottom_square {
width: 150px;
height: 150px;
background-color: #AB877F;
}
I want to make both squares create an alert box when clicked. The top square already achieves this through the HTML code, but I am unable to get the bottom square to create a similar alert through Javascript code. This is what I have tried...
window.onload = init;
function init() {
alert("you have successfully loaded an external .js file.");
// this is just to check that lala.js is loading.
var bottom_sqr = getElementById("bottom_square");
// I also tried calling 'document.getElementById'...
bottom_sqr.onclick = handle_bottom_click;
function handle_bottom_click() {
alert('Bottom square clicked');
}
This is not working at all... hence the post obviously. Any help would be greatly appreciated.