Tag Archives: keyup

Javascript – jQuery – Disable Button Based on Character Length

0
Filed under javascript, jquery
Tagged as , , , , , ,

Author: slac3dork
Site: http://snippet.c0de.me
Summary: This script will disable “Go!” button if total characters more than 140 or have no character using jQuery framework.

<html>
  <head>
    <title>Twitter like text area</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
    <script type="text/javascript">
      $(function(){
          $("#my-submit-id").attr("disabled", "disabled");
          var chars = 0;
          $("#my-text-id").keyup(function(){
            chars = $(this).val().length;
            if ((chars < 1) || (chars > 140)){
              $("#my-submit-id").attr("disabled", "disabled");
            }
            else {
              $("#my-submit-id").removeAttr("disabled");
            }
              $("#my-label").text("Total characters: " + chars);
          });
        });
    </script>
  </head>
  <body>
    <form>
      <label id="my-label">Total characters: 0</label><br />
      <textarea id="my-text-id" name="my-text" rows="5" cols="20">
      </textarea><br />
      <input id="my-submit-id" type="submit" value="Go!" />
  </form>
  </body>
</html>



javascrip,logo,badge,pixel badge