How to change background color of textarea and text field - javascript

I am trying to change the background color of a text box and a text box. All this must be done, when the user presses the button, he activates a function that randomly changes the color of these two components. The code I wrote does not work, could you help me?

<html>
<body>
<textarea id="text"></textarea><br/>
<input type="text" id="c"><br/>
<input type="button" onclick="as()" value="clicca">
</body>

<script>

function as()
{

var t = document.getElementById('text');
var c = document.getElementById('c');

t.style="color: red; background-color: lightyellow"

}

</script>
</html>
+4
source share
2 answers

You can directly change backgroundColor using JavaScript:

t.style.backgroundColor='blue';
+4
source
 t.css("background-color","red")
+1
source

Source: https://habr.com/ru/post/1538273/


All Articles