Why is this event handler not working?
I'm trying to write a script that creates a grid of squares based on user input and then has those squares change colour on mouseover, like some kind of really low res Etch-A-Sketch. Everything is working properly except for the colour changing part. Here's my code (the class 'written' just changes an element's background colour to black):
- var newLine = "<div class='line'></div>";
- var newTile = "<div class='tile'></div>";
- var createLine = function(n) {
- $('#container').append(newLine);
-
- for (var count=1; count<n+1; count++) {
- $('.line').last().append(newTile);
- }
- }
- var createGrid = function(n,m) {
- for (var count=1; count<m+1; count++) {
- createLine(n);
- }
- }
- var userGrid = function() {
- $('#container').empty();
-
- var width = +prompt('Enter grid width:');
- var height = +prompt('Enter grid height:');
-
- createGrid(width, height);
- }
- var colour = function() {
- $(this).addClass('written');
- }
- $(document).ready(function() {
- $('#new-grid').on('click', userGrid);
- $('.tile').on('mouseenter', colour);
- });
When I pass the last event to elements that were already on the HTML file and not generated by jQuery, it works exactly as expected. Any help?