Anton Koenig Anton Koenig - 6 months ago 12
HTML Question

Change color of textarea with Javascript

I have a simple script which is not changing the text color of the textarea. Please see the code below:

<input type="Radio" name="text" onClick="black();">B<br/>
<input type="Radio" name="text" onClick="white();">W
<textarea id="text" placeholder="Explanation (Optional)"></textarea>
<script>
function black() {
document.getElementById("text").style.color="#000000";
}
function white() {
document.getElementById("text").style.color="#ffffff";
}
</script>

Answer

You need to put your javascript within script tags.

See below for working demo.

<input type="Radio" name="text" onClick="black();">B<br/>
<input type="Radio" name="text" onClick="white();">W
<textarea id="text" placeholder="Explanation (Optional)"></textarea>

<script>

function black() {
    document.getElementById("text").style.color="#000000";
}
    function white() {
    document.getElementById("text").style.color="#ffffff";
}

</script>

To avoid putting javascript within script tags you can add you javascript to a .js file and reference this in the html like this <script src="example.js"></script>

Comments