0

I have a function called clear() that gets called when a button in my form gets clicked. JS function:

function clear() {
  console.log("clear");
  document.getElementById("feet").value = '';
  document.getElementById("meters").value = '';
}

HTML:

<button onclick="clear();return false;">Clear</button>

When I click on the Clear button, the clear() function doesn't get called, any ideas why?

Smern
  • 18,746
  • 21
  • 72
  • 90
Cristiano
  • 2,839
  • 8
  • 25
  • 35

4 Answers4

3

Try not using clear(), use another name as @j08691 quoted.

Roger Barreto
  • 2,004
  • 1
  • 17
  • 21
1

I don't know why you use return false in there But ithink if you try using it like this

<button onclick="_clear()">Clear</button>

it will work

Kamal Upasena
  • 1,329
  • 4
  • 13
  • 38
  • return false stops the browsers default behavior: http://stackoverflow.com/questions/128923/whats-the-effect-of-adding-return-false-to-an-onclick-event – Brian Aug 20 '13 at 18:38
  • Oh i see, i thought return false do nothing here, looks like there is something wrong with the document.getElementById("feet").value = ''; – Kamal Upasena Aug 20 '13 at 18:49
0

You can't use clear as the name of the function (Is "clear" a reserved word in Javascript?). The problem is only when you try to call clear from within the document context which shadows the global window context. Change it to _clear and it will work.

function _clear() {
  console.log("clear");
  document.getElementById("feet").value = '';
  document.getElementById("meters").value = '';
}

HTML:

<button onclick="_clear();return false;">Clear</button>
Community
  • 1
  • 1
Brian
  • 3,264
  • 4
  • 30
  • 43
  • clear is NOT a reserved word. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Reserved_Words?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FReserved_Words – Schleis Aug 20 '13 at 18:02
  • Huh, I renamed the function _clear, tested it, and it worked so I just figured it was a reserved word. I didn't even go look it up. Let me change my post, thanks for the feedback. – Brian Aug 20 '13 at 18:05
  • @Brian You can use `clear` as the name of a function. You're wording is still incorrect. The problem is only when you try to call `clear` from within the `document` context which shadows the global `window` context. For example: `window.clear(); return false;` would work just fine (note that I do not recommend either the use of global functions or scripts inside HTML attributes). – Paul Aug 20 '13 at 18:07
  • OK. . . I did say THE function, but you're right it's not clear enough, pun intended. I'm going to steal your wordage and put it in, thanks. – Brian Aug 20 '13 at 18:09
  • Thanks to all for the comments – Brian Aug 20 '13 at 18:10
  • The question remained, what does "document.clear() do". Here it is: "The Document.clear() method does nothing, but doesn't raise any error." (From https://developer.mozilla.org/en-US/docs/Web/API/Document/clear which goes on to say Deprecated: This feature is no longer recommended...) Apparently it only serves those in need of being confused. – captain puget Oct 25 '21 at 23:23
0

The clear() method comes only within the document context and it is deprecated method. We can't use in this context. You can use _clear() or any other alternative method name.

Nandu Hulsure
  • 123
  • 1
  • 7