Assign colors
HTML
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<div id="Content">
<p>
<input class="btn_colors" data-color="#007FFF" type="button" name="blue" id="blue" value="Blue" />
<input class="btn_colors" data-color="#F2B400" type="button" name="yellow" id="yellow" value="Yellow" />
<input class="btn_colors" data-color="#66B447" type="button" name="green" id="green" value="Green" />
</p>
<div class="column">
<div>20</div>
<div>60</div>
</div>
<div class="column">
<div>72</div>
<div>71</div>
</div>
<div class="column">
<div>88</div>
<div>87</div>
</div>
<div class="column">
<div>64</div>
<div>53</div>
</div>
<div class="column">
<div>90</div>
<div>79</div>
</div>
<div class="column">
<div>54</div>
<div>73</div>
</div>
<div class="column">
<div>74</div>
<div>63</div>
</div>
<div class="column">
<div>98</div>
<div>57</div>
</div>
<div class="column">
<div>74</div>
<div>63</div>
</div>
</div>
CSS
.column {
float: left;
}
.column div {
border: 1px solid #000;
padding: 4px;
margin: 2px;
width: 15px;
height: 15px;
text-align: center;
cursor: pointer;
}
JavaScript
// variables
var buttons = document.getElementsByClassName('btn_colors');
var numbers = document.querySelectorAll('.column > div');
var current_color = document.getElementById('green').getAttribute('data-color');
// listener for button clicks
for (let i = 0, c = buttons.length; i < c; i++)
buttons[i].addEventListener('click', set_color, {
passive: false
});
// listener for number cells
for (let i = 0, c = numbers.length; i < c; i++)
numbers[i].addEventListener('click', set_bg, {
passive: false
});
// functions
function set_color(event) {
event.preventDefault();
current_color = this.getAttribute('data-color');
}
function set_bg(event) {
if(this.classList.contains('clicked'))
{
this.classList.remove('clicked');
this.style.backgroundColor = 'transparent';
return ;
}
this.style.backgroundColor = current_color;
this.classList.add('clicked');
}