I have the following HTML:
<form action="cadastro/cadastrando.php" method="post">
<div class="elemf">
<label>Nome</label>
<input id="nome" type="text" name="nome" maxlength="15"/>
<p class="nome_erro">Campo Obrigatório. São permitidos letras e acentos.</p>
</div>
</form>
When I take focus from the text field and it is empty, the message that is with the 'nome_erro'
class should appear. However it does not appear, it seems that the code executes the method below but does not change the value of the display
attribute, continuing 'none'
.
Follow the CSS:
.erro {
font: 12px Verdana, Arial, Helvetica, sans-serif;
color: #ff0000;
left: 10px;
display: inline-block;
}
.nome_erro {
display: none;
}
And the Javascript:
$(document).on('blur','input, textarea, select', function() {
if($(this).val() === '') {
switch($(this).attr('id')) {
case 'nome':
$('.nome_erro').addClass('erro');
break;
}
} else {
switch($(this).attr('id')) {
case 'nome':
$('#nome_erro').removeClass('erro');
break;
}
}
});