Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to change text field color using JavaScript

Tags:

javascript

I'm trying to make the text field change its color but it doesn't seem to work Here is the code:

<html>
<head>
  <title>   
  This is a title
  </title>

<script type="text/javascript">
    function changeColor()
    {
    alert("bla")
    document.getElemenyById("text1").style.background-color:red;
    }
</script>
</head>

<body>

<form>
<input id="text1" type="text" onkeypress="changeColor()">
</form>


</body>

</html>
  • thank you
like image 722
Dani Gilboa Avatar asked Jul 04 '26 22:07

Dani Gilboa


1 Answers

That's a syntax error. You can't use CSS syntax (background-color: red) inside JavaScript.

You want to actually assign a value (the string "red") to a member of the style object called backgroundColor:

...style.backgroundColor = "red";
like image 136
meagar Avatar answered Jul 07 '26 11:07

meagar