Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change background color of textarea and textbox - javascript

Tags:

textarea

I'm trying to change the background color of the textarea and text box. All this must be done when the user presses the button click, it activates a function that changes the color of the two components at random. The code I've written 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>
like image 843
user3344186 Avatar asked Apr 27 '14 13:04

user3344186


People also ask

How can I change background color of textarea in HTML?

For example, if you change the textbox background color to say, blue, you could specify any one of the following: background-color:blue; , background-color:#0000FF; , background-color:rgb(0,0,255); . Actually, you can also use hex shorthand, which would be background-color:#00F; .

How do you change the background color of an input field when text is entered Javascript?

To change the background color on an input field, set the input field's style. background property to a specific color. The backgroundColor property changes the element's background color.

How do I change the background color of a text box in CSS?

To add background color to the form input, use the background-color property.


2 Answers

You can change the backgroundColor directly with JavaScript:

t.style.backgroundColor='blue';
like image 166
diynevala Avatar answered Oct 02 '22 05:10

diynevala


 t.css("background-color","red")
like image 28
Dave Avatar answered Oct 02 '22 04:10

Dave