0

I am calling a function on a button click. But the function is not getting called. I dont know what I am doing wrong. Please help.

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <script type="text/javascript">
    function clear() {
      document.getElementById('inp').value = "";
    }
  </script>
</head>

<body>
  <input type="text" id="inp" name="">
  <input type="button" onclick="clear()">
</body>
</html>

2
  • 1
    clear() is javascript reserve word Commented Sep 21, 2016 at 5:56
  • @NewbeeDev no its not. It just call document.clear instead. I have shared a reference link in my answer. You can check it Commented Sep 21, 2016 at 5:57

3 Answers 3

5

clear is not a reserved word but its calling document.clear instead. Try updating name of function

Sample

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <script type="text/javascript">
    function clear1() {
      document.getElementById('inp').value = "";
    }
  </script>
</head>

<body>
  <input type="text" id="inp" name="">
  <input type="button" onclick="clear1()">
</body>
</html>

Sample - Override document.clear

Note: its a bad practice to override document/window/prototype functions. This is just for demonstration purpose.

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <script type="text/javascript">
    function clear1() {
      document.getElementById('inp').value = "";
    }
    document.clear = function(){
      console.log('My clear function')
    }
  </script>
</head>

<body>
  <input type="text" id="inp" name="">
  <input type="button" onclick="clear()">
</body>
</html>

Reference

Is “clear” a reserved word in Javascript?

Sign up to request clarification or add additional context in comments.

Comments

0

Change the function name because clear function make that issue. make the function name clearInput or what ever you convenient

Comments

0
<!DOCTYPE html>
<html>
<head>
  <title></title>
  <script type="text/javascript">
    function clearTxtBox() {
      document.getElementById('inp').value = "";
    }
  </script>
</head>

<body>
  <input type="text" id="inp" name="">
  <input type="button" onclick="clearTxtBox()">
</body>
</html>

Just change your function name and it will work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.