Changing background-color permanently with .hover()?
I'm working on a mini-project that mimics Etch-a-sketch. I want to change the background color of each dynamically created DIV when the mouse hovers over the element, the change must be permanent. I've spent a couple hours trying to get the color to stick but can't get there on my own, any help will be greatly appreciated!
- $(document).ready(function () {
- var grid = 0;
-
- //Function to create a grid
- function createGrid(g) {
- for (var y = 1; y <= grid; y++) {
- for (var x = 0; x <= grid; x++) {
- if (x != grid) {
- $('#grid').append('<div class="row"></div>');
- } else {
- $('#grid').append('<br><br>');
- }
- }
- }
- }
-
- //Function to generate random colors
- function getRandomColor() {
- var letters = '0123456789ABCDEF'.split('');
- var color = '#';
- for (var i = 0; i < 6; i++ ) {
- color += letters[Math.floor(Math.random() * 16)];
- }
- return color;
- }
-
- //Change color on hover
- $('.row').hover(function(){
- $(this).css('color', getRandomColor());
- })
-
- //Listen for click on submit
- $('#submit').on('click', function (event) {
- event.preventDefault();
- grid = Number($('#input').val());
- createGrid(grid);
- })
- })