Jinzu
Jinzu

Reputation: 1335

Onclick function does not work when doing function call

I have a button that does not call the function when the button is clicked. The onclick function can display an alert window but does not run the function call.

...
   <fieldset>
        <legend>Billing Information</legend>
        <label for="name2">Name:</label><br>
        <input type="text" id="name2" name="full_name"><br>
        <label for="zip2">Zip code:</label><br>
        <input type="text" id="zip2" name="zip_code">
   </fieldset>
   <button type="button" onclick="clear()">Verify</button>
...
<script>
    function clear() {
        document.getElementById("name1").value = "";
        document.getElementById("zip1").value = "";
        document.getElementById("name2").value = "";
        document.getElementById("zip2").value = "";
    }
...
</script>

So this works:

   <button type="button" onclick="alert("Hello")">Verify</button>

But the function clear does not work. I don't see why this doesn't work.

Upvotes: 1

Views: 53

Answers (1)

MarkSkayff
MarkSkayff

Reputation: 1374

This is a picky one.

The problem here is that the word clear is in the scope of document.clear. So, that method is being requested instead of yours.

The solution there would be to use a different word than clear to run that function.

Another solution would be not to use inline event handlers, but to define the listener all in code using javascript.

Here's some documentation on document.clear(). It's in the process of being removed from the Web Standards.

https://developer.mozilla.org/en-US/docs/Web/API/Document/clear

Upvotes: 1

Related Questions