In my javascript code, I want to clear the text fields after done with the operations using onclick command. Question: Is there a predefined fuction to serve this purpose?

Member Avatar for RudyM

If you're using JQuery, something like
$(this).closest('form').find("input[type=text], textarea").val("");

If the initial values when the page is parsed are blank, you can use the .reset() method of the FORM (not the individual inputs), which should revert everything back to what the values were at page render.

If you don't have a form, or are looking for a specific input to reset, (as stated above) you simply have to get the input field and set the value to ""

if it is a <textarea>, in javascript you can go:
document.getElementById("textarea1").innerHTML = "";
This sets the inner HTML of the textarea to nothing, as textareas contain things by the standard <textarea>lorem ipsum</textarea>
But, if it is an <input type="text" /> tag, this is harder. try:
document.getElementById("input1").value = "";
The .value affects the value element of an input tag, and this clears it by setting it to "".

(Which is pretty much what ryantroop said)

hi nblan180131,

if it is a <textarea>, in javascript you can go:
document.getElementById("textarea1").innerHTML = "";

using document.getElementById("textarea1").value = ""; works perfectly for textarea.

Hi hastings.george.5,
I would suggest you use the reset button for form on HTML, since you would probably have more than one text field.

thank you for being helpful. I try all possible suggestions

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.