21-10-2011

jQuery Clear form with jQuery

To clear the text, textareas, passwords, selects, radios and checkboxes, but leave for instance hidden inputs (and any other form-elements not specified in the code below) alone you can safely use the following jQuery code. You can easily add additional field types to clear those also.

$('#searchForm').find(':input').each(function(){
	var type = this.type, tag = this.tagName.toLowerCase();
	if (type == 'text' || type == 'password' || tag == 'textarea')
		this.value = '';
	else if (type == 'checkbox' || type == 'radio')
		this.checked = false;
	else if (tag == 'select')
		this.selectedIndex = -1;
})

Comments:

4 comments.
Your comment:

»
H 16/11/2011, 12:18 pm
Nice code....
xd 04/09/2012, 11:46 am
fd
Anuj 09/10/2012, 2:22 pm
Ok Thank you
Testeer 31/10/2012, 10:38 am
Not good way

 

[x]