Clear value HTML JAVASCRIPT how do do it

Learn how to create clear value with an input field and textarea field.
1 input field :

Code;

<!-- When the input field gets clicked, replace its current value with an empty string -->

<input type="text" onfocus="this.value=''" value="Rose">


Output:


 


Textarea clear button field by clearing the text you put on the box with one click

Code:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
  </head>

  <body>
 
    <textarea id="message" name="message" rows="5" cols="33"></textarea>

    <button id="btn">Clear value</button>

    <script src="index.js"></script>
  </body>
</html>

<script>
  const textarea = document.getElementById('message');

//  Clear textarea value
textarea.value = '';

//  Clear textarea value on click
const btn = document.getElementById('btn');

btn.addEventListener('click', function handleClick() {
  //  log value before clearing it
  console.log(textarea.value);

  //  clear textarea value
  textarea.value = '';
});

</script>

Output:


Comments :

Post a Comment