Hello,
With the function below, I validate required fields.
In this case, I do not use this function to submit the form, but rather to add items to the object.
However, I want to focus on the first required element.
Something like:
$('#elemento')[0].focus
.
Can you help me?
campos = ['#txt-cep', '#txt-logradouro', '#txt-bairro', '#txt-cidade', '#txt-uf'];
if (campo_obrigatorio(campos)) {
//faça algo
}
function campo_obrigatorio(campos) {
var ret = true;
for (i = 0; i < campos.length; i++) {
var $form_line = $(campos[i]).parent();
if (!$(campos[i]).val()) {
if (!$form_line.hasClass('error focused')) {
$form_line.addClass('error focused');
//$form_line.find($(campos[i]).focus());
}
ret = false;
}
else {
if ($form_line.hasClass('error focused')) {
$form_line.removeClass('error focused');
}
}
}
return ret;
}