forked from iaac-macad-s1/lecture1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
61 lines (43 loc) · 1.43 KB
/
script.js
File metadata and controls
61 lines (43 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// get reference to sliders
const x_slider = document.getElementById( 'x' )
const y_slider = document.getElementById( 'y' )
// listen to slider events
function makeRows() {
// remove existing table
let table = document.getElementById( 'table' )
if ( table ) {
table.remove()
}
// create new table
table = document.createElement( 'table' )
table.id = 'table'
document.body.appendChild( table )
// get slider values
const columns = x_slider.valueAsNumber
const rows = y_slider.valueAsNumber
// update labels
document.getElementById( 'x_label' ).innerHTML = 'X: ' + columns
document.getElementById( 'y_label' ).innerHTML = 'Y: ' + rows
// make table items
for ( i = 0; i < rows; i ++ ) {
// create a table row
const row = document.createElement( 'tr' )
table.appendChild( row )
for ( j = 0; j < columns; j ++ ) {
// create a table cell
const cell = document.createElement( 'td' )
cell.innerText = i + ',' + j
// uncomment to generate a random background color for cell on mouseover
const color = Math.floor( Math.random() * 16777215 ).toString( 16 )
cell.addEventListener( 'mouseover', function () {
cell.style.background = '#' + color
}, false )
cell.addEventListener( 'mouseout', function () {
cell.style.background = 'white'
}, false )
row.appendChild( cell )
}
}
}
// run once to initialise table
makeRows();