admin管理员组文章数量:1429430
I am creating a table via javascript and one column will contain an html color picker. The problem that I have is that in Chrome the default value is set but the color that is displayed on the color picker is black.
This is the javascript that I am using
c = r.insertCell(1);
c.setAttribute('class','second');
inp=document.createElement("input");
inp.setAttribute('type','color');
inp.setAttribute('id','colo_'+i);
inp.setAttribute('value','#ffffff');
inp.setAttribute('class','datafield');
inp.addEventListener('change', saveFields);
c.appendChild(inp);
This is the html that is generated from if
<td class="second">
<input type="color" id="colo_0" value="#8080ff" class="datafield">
</td>
Is this a bug or am I doing something wrong?
I am creating a table via javascript and one column will contain an html color picker. The problem that I have is that in Chrome the default value is set but the color that is displayed on the color picker is black.
This is the javascript that I am using
c = r.insertCell(1);
c.setAttribute('class','second');
inp=document.createElement("input");
inp.setAttribute('type','color');
inp.setAttribute('id','colo_'+i);
inp.setAttribute('value','#ffffff');
inp.setAttribute('class','datafield');
inp.addEventListener('change', saveFields);
c.appendChild(inp);
This is the html that is generated from if
<td class="second">
<input type="color" id="colo_0" value="#8080ff" class="datafield">
</td>
Is this a bug or am I doing something wrong?
Share Improve this question asked Oct 10, 2013 at 7:06 stephan2307stephan2307 3571 gold badge4 silver badges17 bronze badges2 Answers
Reset to default 4Interesting. I was able to replicate your results (example).
Rather than using setAttribute
, set the value
directly:
inp.value = '#ffffff';
Apparently that makes Chrome happy. (Live Copy | Source)
Side note: All of the things you're setting via setAttribute
have reflected properties you can use instead:
inp = document.createElement("input");
inp.type = 'color';
inp.id = 'colo_'+i;
inp.value = '#ffffff';
inp.className = 'datafield';
Note that the last one is className
rather than class
.
Updated Example | Source
It might be helpful for someone someday, so I'll just put it here: The value of input color seems to work only with hex value (rather than RGB) - so if your color is in RGB - switch it to hex first. RGB to hex
本文标签: javascriptHTML5 input type color doesn39t show value when created via jsStack Overflow
版权声明:本文标题:javascript - HTML5 input type color doesn't show value when created via js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745443344a2658546.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论