Last active
December 16, 2015 05:59
-
-
Save jackmcmorrow/5388796 to your computer and use it in GitHub Desktop.
A CodePen by jackmcmorrow. Limitador de caracteres - É um simples limitador de caracteres em JS com um pouquinho de jQuery na execução. Ele cria um objeto novo toda a vez que você clica em um campo, isso deve pesar menos apesar do seletor na classe dos elementos. #Update#
Já que eu fiz o negócio, melhor deixar direito: joga um atributo limite no…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="span4"> | |
<div class="control-group"> | |
<label for="coment" class="control-label">Comentário simples <br></label> | |
<div class="controls"> | |
<textarea class="comentarioGrupo validarGrupos" name="coment" id="coment" cols="50" rows="5" limite="200"></textarea> | |
<span class="help-block"><b>200</b> caracteres restantes</span> | |
<label for="comentarioGrupo" class="label label-important"></label> | |
</div> | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
function CampoLimitado(campo, limite) { | |
this.campo = campo; | |
this.contagem = campo.nextElementSibling.childNodes[0]; | |
this.limite = limite; | |
this.contar = function() { | |
this.tamanho = campo.value; | |
if (this.tamanho.length > this.limite) { | |
campo.value = campo.value.substring(0, limite); | |
} | |
else { | |
if (this.limite - this.tamanho.length < limite / 2) { | |
this.contagem.classList.add("desempenho-baixo") | |
} else { | |
this.contagem.classList.remove("desempenho-baixo") | |
} | |
this.contagem.textContent = this.limite - this.tamanho.length; | |
} | |
}; | |
}; | |
$('.comentarioGrupo').on('focus', function() { | |
var campoSelecionado = new CampoLimitado(this, $('this').attr('limite')); | |
campoSelecionado.contar(); | |
$(this).on('keydown', function() { | |
campoSelecionado.contar(); | |
var parentCampo = $(campoSelecionado.campo).parent(); | |
var stringCampo = campoSelecionado.tamanho; | |
if (stringCampo.length == 2) { | |
$('.label', parentCampo).html(''); | |
} | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.span4 { | |
margin: 0 auto; | |
width: 30%; | |
} | |
.controls { | |
width: 100%; | |
} | |
input { | |
float: left; | |
} | |
.help-block { | |
clear: both; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment