Working fiddle : https://jsfiddle.net/pckshu27/3/
here to achieve your requirement, you need to add up one additional block under JS and a minor change under present JS.
Use of this $(colorCells[i]).attr("bgColor", colors.splice(Math.random() * (colors.length),1)) ;
instead of colorCells[i].style.backgroundColor = colors.splice(Math.random() * (colors.length),1);
The above change has to be done, because using bgcolor under style tag, retrieval makes difference, as it returns under RGB
format of color code, so instead you can use bgColor
an attribute under TD
, this makes easy retrieval and without change.
Additional JS block
var colorValues = {"red": 2, "blue":3, "green": 4, "yellow":"", "orange":5, "black":1, "brown":6, "pink":5};
$("table td").each(function() {
$(this).html(colorValues[$(this).attr("bgColor")]);
});
the above code considers of an array with required keys and values, and pretty simple to be known, as if the attr bgColor
value is red
, it finds the key under the array, adds the value of Array[red]
as the html under the TD
. you can even use .text()
instead of .html()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…