Menu
I need code When I Click to Button it should clear all inpt box and combox.listindex=-1 on the document. Can Some body help me ??
<input type="reset" value="Clear All" onclick="document.forms[0].combox.selectedIndex = -1">
Doesn't simply this work?
<input type="reset" value="Reset">[/QUOTE]
That will only reset a form to its original state.[/QUOTE]
[CODE]
function ClearForm(formIdent)
{
var form, elements, i, elm, element;
form = document.getElementById
? document.getElementById(formIdent)
: document.forms[formIdent];
if (document.getElementsByTagName)
{
elements = form.getElementsByTagName('input');
for( i=0, elm; elm=elements.item(i++); )
{
if (elm.getAttribute('type') == "text")
{
elm.value = '';
}
}
}
// Actually looking through more elements here
// but the result is the same.
else
{
elements = form.elements;
for( i=0, elm; elm=elements[i++]; )
{
if (elm.type == "text")
{
elm.value ='';
}
}
}
}
function addEvent(elm, strEvent, fnHandler)
{
return ( elm.addEventListener
? elm.addEventListener( strEvent, fnHandler, false)
: elm.attachEvent( 'on'+strEvent, fnHandler)
);
}
[/CODE]
i just dont need on reset button i want to use on many buttons so i need function or there is any way to call reset function call[/QUOTE]
<i>
</i>// js
<i> </i>function clearForm(frm) {
<i> </i> for (var elIdx=0;elIdx<frm.elements.length;elIdx++) {
<i> </i> if (frm.elements[elIdx].type!="hidden") {
<i> </i> if (frm.elements[elIdx].selectedIndex) frm.elements[elIdx].selectedIndex = -1;
<i> </i> else if (frm.elements[elIdx].checked) frm.elements[elIdx].checked = false;
<i> </i> else if (frm.elements[elIdx].value) frm.elements[elIdx].value = "";
<i> </i> }
<i> </i> }
<i> </i>}
// html
<i> </i><input type="button" value="clear all" onclick="clearForm(this.form)"/>
function clearForm(frm) {
var els = frm.getElementsByTagName('*');
for(var i=0;i<els.length;i++)
{
if(els[i].type=='text'||'textarea')
els[i].value='';
if(els[i].nodeName=='select')
els[i].selectedIndex=-1;
}
return true;
}
thanx ricp to reply but it not working and giving error that frm.elements is null. What can be solution for that.[/QUOTE]
<i>
</i><input type="button" onclick="clearForm(document.getElementById('myFormID'))" value="clear all"/>
Or since the OP asked to clear textareas, textboxes, and select objects . . . and not checkboxes . . . simply:
[/QUOTE]
You are not passing the correct form element back. I assumed that the button you clicked was within the <form> that it was trying to clear. That's what the this.form refers to. I did say that it was difficult to second guess you without seeing your HTML.
If the button is outside the <form> you wish to clear then you would need to pass something like...
<i>
</i><input type="button" onclick="clearForm(document.getElementById('myFormID'))" value="clear all"/>
You shouldn't really have a name attribute on your form, hence why I refer to it's id attribute in the code above.
Nope he didn't but he didn't mention he wanted textarea's cleared in his first post either. If you are clearing selects, inputs and the like, it's a fair assumption that all form elements are to be cleared. The thread is also called "Clear All", the emphasis being on "all".. :rolleyes:
Btw, your code is inefficient. Why are you using the .getElementsByTagName() method when formObj.elements is already available without any calls? With the former you will pick up every tag within the form, including tables, divs, images, etc. At least with the .elements collection you are restricting the number you loop through.[/QUOTE]
Like you sid, the OP asked for "Clear all", and also, it doesnt matter which one I use because I check for specific element types, thus tables and divs would not be affected. I already know about the .elements array collection, but why limit yourself?
[/quote]
Aso, there is no reason why you shouldn't have a name attribute on a form. It makes easier to access without using getElementById(). I prefer to use IDs, but there is no reason why you shouldnt use a name.
[/quote]
Because it's inefficient. Take for example the code had several layers of nested tables (bad html, but just for this example) then the loop will still go through them. I appreciate you are not setting values on it, but the length of the collection will be bigger and thus the processing overhead larger. It's in no way "wrong", just long winded.
The elements collection is there for the specific task of referring to form elements, not a case of limiting yourself but using the logical collection that exists within the browser already rather than making another call to the processor for the .getElementsByTagName() method.
Again it's a case of "you are not wrong", but there is a very significant reason why you shouldn't use it. From the introduction of xhtml the name attribute has been deprecated on all elements barring specific elements within the form tag. If we (as a JavaScript community) are to promote standards and push forward compatibility it surely must be in the best interests to stop using such outdated methods. HTML4 is over 10 years old now and really should be resigned to the bin.[/QUOTE]
Most of what you say is true, but just to give an example, if the user has input objects that alter the code for a table in a form, then most likely when you clear the form you would also want to clear out the table . . . you cannot do that with .elements you can however do it with getElementsByTagName('*')
[/QUOTE]
On the subject of names being deprecated in XHTML, the name attribute, or "XHTML Name Identification Module" is available for all elements, but at times it is better to use IDs. Most commonly for elements such as <a> tags, images, or frames. Some people also think that it is better to use IDs for forms, which I agree is best, but if you read any documentation about the Name Module in XHTML you will usually come across a line saying that it is used in most instances, but replaced with the ID Module when more effective.
[/quote]
4.10. The elements with 'id' and 'name' attributes
HTML 4 defined the name attribute for the elements a, applet, form, frame, iframe, img, and map. HTML 4 also introduced the id attribute. Both of these attributes are designed to be used as fragment identifiers.
Note that in XHTML 1.0, the name attribute of these elements is formally deprecated, and will be removed in a subsequent version of XHTML.
[/quote]
Unfortunately, people still use older browsers, so it is usually best to include a name and an ID attribute. That way we can cater to the needs of both newer and older browsers.
[/quote]
0.1.9 — BETA 5.19