i have a form with an id="test"
. Inside the form i have some select
, input
and textarea
fields.
i want to iterate through each of them and check if they are empty and then do something with them.
var editTest= $('#test');
editGeneric(editTest);
var editGeneric= function(form){
form.children().find('select').each(
function(){
var values = $(this).val();
if(values === 'select one' || values === 'Select One' || values == 0 || values == 99){
$(this).css('border', '1px solid red');
}
}
);
form.children().find('input').each(
function(){
var values = $(this).val();
if(values === ''){
$(this).css('border', '1px solid red');
}
}
);
form.children().find('textarea').each(
function(){
var values = $(this).val();
if(values === ''){
$(this).css('border', '1px solid red');
}
}
);
}
this code does what i need, but you can see that i repeat myself a few times.
how can i simplify this code so i can maybe use it with some other forms?
any ideas?, thanks
edit: thanks all
i got the code down to:
var editGeneric= function(form){
form.children().find(':input').each( function(){
var values = $(this).val();
if ($(this).is('select')){
if(values === 'select one' || values === 'Select One' || values == 0){
$(this).css('border', '1px solid red');
}
}
if ($(this).is('input') || $(this).is('textarea')){
if(values === ''){
$(this).css('border', '1px solid red');
}
}
}
);
}
editGeneric($('test'));