cdiego

contador de caracteres dinámico JQuery

Sep 12th, 2013
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.48 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <title>Contador de caracteres</title>
  5.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  6.         <script type="text/javascript" src="http://code.jquery.com/jquery-2.0.3.min.js" ></script>
  7.         <style>
  8.             #contador{
  9.                 height: 10px;
  10.                 width: 0px;
  11.                 position: absolute;
  12.                 display: none;
  13.                 background: green;
  14.                 border-radius: 5px;
  15.             }
  16.             #porcentaje{
  17.                 width: 50%;
  18.                 height: 100%;
  19.                 background: red;
  20.                 border-radius: 5px;
  21.             }
  22.         </style>
  23.         <script>
  24.         $(document).on("ready", function(){
  25.             //ELEMENTOS DEL CONTADOR
  26.             //PARTE VERDE
  27.             var contador = $("<div/>").attr("id", "contador").appendTo("body");
  28.             //PARTE ROJA
  29.             var porcentaje = $("<div/>").attr("id", "porcentaje").appendTo(contador);
  30.            $("textarea").on("click mouseover focus", function(){
  31.                //OPERACIONES DEL CONTADOR
  32.                var maxlength = $(this).attr("maxlength");
  33.                var total_letras = $(this).val().length;
  34.                var cantidad_porcentaje = (total_letras * 100)/ maxlength;
  35.                contador.find("#porcentaje").css("width", cantidad_porcentaje+"%");
  36.                var ancho = $(this).width();
  37.                var posicion_contador = $(this).offset().top + $(this).height() + 5;
  38.                contador.css({
  39.                    "top":posicion_contador,
  40.                    "width":ancho
  41.                }).fadeIn("fast");
  42.            }).on("keyup", function(){
  43.                //OPERACIONES DEL CONTADOR
  44.                var maxlength = $(this).attr("maxlength");
  45.                var total_letras = $(this).val().length;
  46.                var cantidad_porcentaje = (total_letras * 100)/ maxlength;
  47.                contador.find("#porcentaje").animate({
  48.                    "width": cantidad_porcentaje+"%"},
  49.                     {
  50.                         duration:"fast",
  51.                         queue:false
  52.                     });
  53.            });
  54.         });
  55.         </script>
  56.     </head>
  57.     <body>
  58.         <textarea rows="10" cols="20" maxlength="20">maxlength 20</textarea><hr/>
  59.         <textarea rows="10" cols="40" maxlength="40">maxlength 20</textarea><hr/>
  60.         <textarea rows="10" cols="60" maxlength="60">maxlength 20</textarea><hr/>
  61.     </body>
  62. </html>
Advertisement
Add Comment
Please, Sign In to add comment