javascript - convert decimal to hex in table -
i trying create table similar one:
i uploaded site can viewed here: http://alainwebdesign.ca/cis245/rgb.html
in script file, trying populate "red hex" column modifying generaterandomreds()
function so:
function generaterandomreds(){ for(var i=0; i<11; i++){ var randred = math.random(); randred = randred.tofixed(2); var sel = document.getelementbyid('genrandomreds'); var opt = document.createelement('option'); opt.innerhtml = randred; opt.value = randred; sel.appendchild(opt); //trying convert decimal hex , populate "red hex" //column in generate random colors table //but reason adding code makes 1 random //red decimal value created , that's it: var selhex = document.getelementbyid('genredhex'); var opthex = document.createelement('option'); opthex.innerhtml = randred(parseint( number , 10)).tostring(16); opthex.value = randred(parseint( number , 10)).tostring(16); selhex.appendchild(opthex); } }
a created following code explain:
html:
<button type="button" class="btn btn-default" id="generate-btn">genarate random colors</button> <br/><br/> <table class="table"> <thead> <tr> <th>rgb values</th> <th>rgh hexadecimal values</th> <th>display</th> </tr> </thead> <tbody id="tbody"> </tbody> </table>
javascript:
document.getelementbyid('generate-btn').addeventlistener("click", generaterandom); var tbody = document.getelementbyid('tbody'); function generaterandom() { tbody.innerhtml = ''; (var = 0; < 11; ++i) { var randr = (math.random().tofixed(3) * 1000) % 256; var randg = (math.random().tofixed(3) * 1000) % 256; var randb = (math.random().tofixed(3) * 1000) % 256; var rgbvalue = '(' + randr + ', ' + randg + ', ' + randb + ')'; var tdf = '<td>' + rgbvalue + '</td>'; var hexvalue = '#' + randr.tostring(16) + randg.tostring(16) + randb.tostring(16); var tdh = '<td>' + hexvalue + '</td>'; var display = ''; var tdd = '<td style="background-color:' + hexvalue + '"></td>'; var tr = '<tr>' + tdf + tdh + tdd + '</tr>' tbody.innerhtml = tbody.innerhtml + tr; } }
in example i'm generating integers rbg numbers. rgb goes 0 255 created random numbers 3 digits max value of 255. convert hexadecimal simple use tostring(16).
see following fiddle: https://jsfiddle.net/alissonmbr/9nb1zq1g/
Comments
Post a Comment