Ven Nilson
Ven Nilson

Reputation: 1019

Javascript Clear fields Function Not Working?

This is function that i created for clear text fields but when enter any custom values it doesn't clear

function clear(){

    document.getElementById('bmw1').value="";
    document.getElementById('bmw2').value="";
    document.getElementById('ans').value="";


}

The fields which created in html

<input type="text" id="bmw1" placeholder="Enter 1st Number"/>
<input type="text" id="bmw2" placeholder="Enter 2nd Number"/>
<input type="text" id="ans" placeholder="Answer"/>
<button type="button" onClick="clear()">Clear Values</button>

Upvotes: 2

Views: 1778

Answers (4)

madoxdev
madoxdev

Reputation: 3880

Problem is with name of function which is already reserved for Document.

Please change your function name to other and it will work well.

Upvotes: 0

Ian Wise
Ian Wise

Reputation: 766

I believe JavaScript already has a clear() function, try renaming your method. The following works for me :

function erase(){
        document.getElementById('bmw1').value = "";
}

Upvotes: 0

The Guest
The Guest

Reputation: 708

You need to change the js function name from clear() to something else. Because clear() is a java script built in function/method.

Upvotes: 1

Gary Storey
Gary Storey

Reputation: 1814

Why re-invent the wheel? You can do that with basic HTML:

<input type="reset" value="Reset" />

Just make sure that is inside your form and it will clear all the values.

Upvotes: 0

Related Questions