Help with function please


Joined
Apr 14, 2020
Messages
2
Reaction score
0
Hi,
I'm trying to create a function that will change the <body> background color (from GIF to color) if the user types text inside <textarea>.
I'm a complete beginner so I'm a bit lost here..
I tried with something like this but it doesn't really work:

HTML:
<!DOCTYPE html>
<html>
<body>
<textarea class="input" id="myTextarea">
</textarea>
<script>
const textArea = document.querySelector(".input");
function myFunction() {
  if ( textArea.value == "" )
  {
      document.body.style.backgroundImage = url('[URL]https://scx1.b-cdn.net/csz/news/800/2016/trackingwave.gif[/URL]');
  }
      else if (textArea != "" ) {
      document.body.style.backgroundColor = "black";
  }
}
myFunction()
</script>
</body>
</html>
 
Last edited by a moderator:
Ad

Advertisements

Joined
Nov 27, 2019
Messages
127
Reaction score
19
Easiest way
Code:
<!DOCTYPE html>
<html>
<head>
  <style>
  body{background-image: url('01.jpg');}
  </style>
</head>
<body>
  <textarea class="input" id="myTextarea" oninput="myFunction()">
  </textarea>
<script>
function myFunction() {
      document.body.style.background = "black";
}
</script>
</body>
</html>
 
Ad

Advertisements

Joined
Apr 14, 2020
Messages
2
Reaction score
0
Easiest way
Code:
<!DOCTYPE html>
<html>
<head>
  <style>
  body{background-image: url('01.jpg');}
  </style>
</head>
<body>
  <textarea class="input" id="myTextarea" oninput="myFunction()">
  </textarea>
<script>
function myFunction() {
      document.body.style.background = "black";
}
</script>
</body>
</html>

Thanks! is there an option to return the original background GIF if the user delets the input from the TEXTAREA?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top