2

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>

4 Answers 4

1

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

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

Comments

0

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.

1 Comment

I already Know this reset button but is there a way that we clear fields with function
0

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

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

Comments

0

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

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

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.