top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to clear all text elements through javascipt?

+2 votes
592 views

I have a .js file in which i have defined the function as shown.

function resetall(){    
  //document.write("Hello") ------------This one is working
  var elements = document.getElementsByTagName("input");
  for (var i=0; i < elements.length; i++) {
   if (elements[i].type == "text") {
     elements[i].value = "";
   }
  }
}

I have a resetall button in .html file

<button type="button" value="Reset" onclick="javascript:resetall();">RESET</button>

I tested the reset button by putting a document.write("Hello") to ensure that function is being invoked or not, and it is all right, But text fields are not being cleared. I am not able to figure out what is the problem. Even I have tested document.getelementbyId().

posted Mar 12, 2014 by Prakash

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
I tested it with getElementByName() and getElementById().
The element which i am trying to access is accessible, for example i tried to disable a input field... like
document.getElementById("First  name").disabled=true.
It worked but when I wrote
document.getElementById("First  name").value="";
 it didn't.


Any Help??
Again comments are poorly drafted, I hope I got the problem. Try something
document.getElementById("myid").innerHTML="New text"; // provide the foll inner html here.

1 Answer

+1 vote

Not sure if I got your problem or not try something like this -

<div id="myid" name="myname">
<button type="button" value="Reset" onclick="javascript:resetall();">RESET</button>
</div>

Now search using
document.getElementsByName("myname");
or
document.getElementsById("myid");

I hope it should work.

answer Mar 12, 2014 by Salil Agrawal
Similar Questions
0 votes

Write validation functions and modify the onSubmit event Handler in the form code to validate the following form fields:

  1. Phone Number
    • Must be entered
    • Must be below format
    XXX-XXX-XXXX
    XXX.XXX.XXXX
    XXX XXX XXXX
+3 votes

I am trying to debug a javascript file, which is used in a .jsp file. When I debug the using F12 in IE10 I did not find that particular file in the debugger tool.

Can anybody please tell, why that particular file is not showing even it is present in project folder location?

...