I have the following code that gets input from users on a button click and draws on a canvas but when i click the button nothing works. Here's the code:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="mycss.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js.js"></script>
<title>CANVAS</title>
</head>
<body>
<canvas id="myCanvas" width="800" height="600" style="border: 1px solid #000000;"></canvas>
<form>
<input id="rectangle" type="button" value="Draw a rectagle" class="button2"/>
</form>
</body>
</html>
js.js
function drawRectangle(){
var x = prompt("Enter x co-ordinate", "0");
var y = prompt("Enter y co-ordinate", "0");
var color = prompt("Enter color to fill rectangle","color");
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
context.fillStyle = color;
context.fillRect(x,y,100,50);
}
function registerEvents(){
var rect = document.getElementById( "rectangle" );
rect .addEventListener( "click", drawRectangle, false);
}
window.addEventListener(load, registerEvents, false);
I don't know why the code doesn't work. What i'm i getting wrong? Thanks