How to change background color of a textarea using JavaScript
I just want to know how can I change the background colour of a textarea to a colour typed in a text box. I've managed to do the text colour, font and size, but doing the background the same way doesn't seem to work. My code for the text colour and background is:
Script:
function setColor(where, Color)
{
if (where == "backgroundcolour")
document.getElementById('textarea').style.backgroundColor = Color;
if (where == "colourtext")
document.getElementById('textarea').style.color = Color;
}
HTML:
<p>
Card color: <input type = "text" name = "backgroundcolour"
size = "10"
onchange = "setColor('backgroundcolour',
this.value)">
<br>
Text color: <input type = "text" name = "colourtext"
size = "10"
onchange = "setColor('colourtext',
this.value)">
<br>
</p>
<textarea id = 'textarea' name="data" cols="100" rows="10">
</textarea>
it seems as though my internet was blocking the script so it wouldnt change the backgrou开发者_StackOverflow中文版nd
Your code is actually working. Maybe you forgot to remove the focus from the text box in order to get the change event triggered.
function setColor(where, Color)
{
if (where == "backgroundcolour")
document.getElementById('textarea').style.backgroundColor = Color;
if (where == "colourtext")
document.getElementById('textarea').style.color = Color;
}
<p>
Card color: <input type = "text" name = "backgroundcolour"
size = "10"
onchange = "setColor('backgroundcolour',
this.value)">
<br>
Text color: <input type = "text" name = "colourtext"
size = "10"
onchange = "setColor('colourtext',
this.value)">
<br>
</p>
<textarea id = 'textarea' name="data" cols="100" rows="10">
</textarea>
精彩评论