Is there a simple way to iterate over the child elements in an element, say a div, and if they are any sort of input (radio, select, text, hidden...) clear their value?
Edit to add link to example solution code. Many thanks to Guffa and the other respondents! I learned from this!
This is an old post but someone can see it.
function clearFields(divElement) {
var ancestor = document.getElementById(divElement),
descendents = ancestor.getElementsByTagName('*');
var i, e, d;
for (i = 0; i < descendents.length; ++i) {
if (descendents[i].tagName.toLowerCase() == 'input'){
switch (descendents[i].type){
case 'text':
case 'password':
case 'color':
case 'date':
case 'email':
case 'month':
case 'number':
case 'range':
case 'search':
case 'tel':
case 'time':
case 'url':
case 'week':
descendents[i].value = '';
break;
case 'radio':
case 'checkbox':
descendents[i].checked = false;
}
}
else if (descendents[i].tagName.toLowerCase() == 'select'){
descendents[i].selectedIndex = 0;
}
}
}
and to use it:
clearFields ('divName');
Of course you can add more elements to rest it
Very simple with JQuery:
$('#myDiv').children().find('input,select').each(function(){
$(this).val('');
});
We can put as many tags in the "find" call.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With