简体   繁体   中英

input type color in html5 to have random value

can i make my color value random? i have a for loop that creates input fields color,and i want that i have random values of color when they are created. can i do this with input type=color in html5? or not? what i tried

for(var i=0;i<val;i++)
    {
       randomColor=‘#’+(Math.random()*0xFFFFFF<<0).toString(16); 
       generate(e, i, randomColor);  
    }

function generate(e, i,randomColor)
    {   

    e.innerHTML += "<input type='color' name ='color1' value="randomColor" id='color"+i+"'/>";

}

Well you are building your string wrong, it should be like how you do the id.

e.innerHTML += "<input type='color' name ='color1' value='" + randomColor + "' id='color"+i+"'/>";
                                                         ^^^^^^^^^^^^^^^^^^^^^

and you have "fancy quotes"

randomColor=‘#’
            ^^^
for(var i = 0; i < val; i++) {
   randomColor = '#'+(0x1000000+(Math.random())*0xffffff).toString(16).substr(1,6);
   generate(e, i, randomColor);  
}

function generate(e, i, randomColor) {   
   e.innerHTML += "<input type='color' name ='color1' value='" + randomColor + "' id='color"+i+"'/>";    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM