How to clear text box on focus in JavaScript?

How to clear text box on focus in JavaScript?

You can use the onfocus attribute in JavaScript to clear a textbox or an input box when somebody sets focus on the field. If you are using jQuery, then use the .focus () method to clear the field.

How does an element lose focus in jQuery?

Originally, this event was only applicable to form elements, such as . In recent browsers, the domain of the event has been extended to include all element types. An element can lose focus via keyboard commands, such as the Tab key, or by mouse clicks elsewhere on the page.

How to clear an input field in jQuery?

If the inline method does not suit your requirement, then you can call a function to clear the input field. For example, Alternatively, you can use the jQuery .focus () method to clear an input field, if in your application you are using jQuery. There are different ways to do this.

How to clear an input field on focus?

Learn how to clear an input field on focus. Tip: Go to our HTML Form Tutorial to learn more about HTML Forms. Thank You For Helping Us! Your message has been sent to W3Schools. W3Schools is optimized for learning, testing, and training.

How to clear the text area in JavaScript?

We have also created a button named ” Clear TextArea ” which calls clearTextArea () method defined in . It clears the value of the text area. The above code will display the page like below: Click on ‘Clear TextArea’ button, you will see that the text is not present in the text area.

How to clear value of text field on click?

Inline JavaScript to clear value of text field on click is really simple but works great. As you should know HTML’s attribute onClick actually calls the JavaScript means you can run JavaScript with onClick attribute. Let’s take an example of inline JavaScript to clear the value.

How to clear default text field in JavaScript?

But if you have large or multiple forms then its recommended to use Form for clearing default text field, textarea values. Tip: Instead of giving string to compare you can also use this.defaultValue defaultValue is a JavaScript function which returns the value of field which loaded with HTML from server.

About the Author

You may also like these