TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Clear All Controls Using a single Javascript function
Ajith Mohan
Jan 07
2015
Code
4.2
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
function
ClearTextbox(ParentControl) {
var
control = $(ParentControl).get(0);
var
list = control.getElementsByTagName(
"input"
);
var
textAreaList = control.getElementsByTagName(
"textarea"
);
var
selectList = control.getElementsByTagName(
"select"
);
for
(
var
i = 0; i < list.length; i++) {
var
attr = list[i].getAttribute(
'type'
);
var
control = document.getElementById(list[i].id);
switch
(attr) {
case
"text"
:
$(control).val(
''
);
break
;
case
"hidden"
:
$(control).val(
''
);
break
;
case
"checkbox"
:
control.checked =
false
;
break
;
case
"radio"
:
control.checked =
false
;
break
;
case
"range"
:
control.setAttribute(
'value'
,
'0'
);
break
;
case
"file"
:
control.setAttribute(
'value'
,
''
);
break
;
}
}
for
(
var
i = 0; i < textAreaList.length; i++) {
var
control = document.getElementById(textAreaList[i].id);
$(control).val(
''
);
}
for
(
var
i = 0; i < selectList.length; i++) {
$(selectList[i]).val(
''
);
}
}
Make a Call to the function.
<script>
$(
'#btnClear'
).click(
function
(e) {
e.preventDefault();
ClearTextbox(
'#form'
);
//Pass the Parent control ID of Controls u want to clear
//Here I have Passes the ID of the forms tag clear all the controls in the form.
//In Case you want to clear only the contents in a Particular div or table, we can pass the ID of that div or //table and it will clear the elements only in that div or table.
});
</script>
Controls
javascript
Javascript function