Thursday, October 10, 2019

Displaying The Character Count of a Textarea - JavaScript




<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <style>    
        **:before*:after {
            box-sizingborder-box;
        }

        html {
            font-size100%;
        }

        body {
            font-family'Open Sans'sans-serif;
            font-size16px;
            background#d0e6ef;
            color#666;
        }

        .wrapper {
            max-width75%;
            marginauto;
        }
        textarea {
  width100%;
  min-height100px;
  resizenone;
  border-radius8px;
  border1px solid #ddd;
  padding0.5rem;
  color#666;
  box-shadowinset 0 0 0.25rem #ddd;
  &:focus {
    outlinenone;
    border1px solid darken(#ddd, 5%);
    box-shadowinset 0 0 0.5rem darken(#ddd, 5%);
  }
  &[placeholder] {
    font-styleitalic;
    font-size0.875rem;
  }
}

        h1 {
            color#555;
            margin3rem 0 1rem 0;
            padding0;
            font-size1.5rem;
        }
        #the-count {          
            padding0.1rem 0 0 0;
            font-size0.875rem;
        }
    </style>
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            var text_max = 250;

            $('#textarea').keyup(function (event) {
                if (!checkSpecialKeys(event)) {
                    var text_length = $('#textarea').val().length;
                    current = $('#current'),
                    maximum = $('#maximum'),
                    theCount = $('#the-count');
                    current.text(text_length);
                    /*This isn't entirely necessary, just playin around*/
                    if (text_length < 70) {
                        current.css('color''#666');
                    }
                    if (text_length > 70 && text_length < 90) {
                        current.css('color''#6d5555');
                    }
                    if (text_length > 90 && text_length < 100) {
                        current.css('color''#793535');
                    }
                    if (text_length > 100 && text_length < 120) {
                        current.css('color''#841c1c');
                    }
                    if (text_length > 120 && text_length < 139) {
                        current.css('color''#8f0001');
                    }

                    if (text_length >= 140) {
                        maximum.css('color''#8f0001');
                        current.css('color''#8f0001');
                        theCount.css('font-weight''bold');
                    } else {
                        maximum.css('color''#666');
                        theCount.css('font-weight''normal');
                    }

                }
            });

        });
        /*
        Checks if the keyCode pressed is inside special chars
        -------------------------------------------------------  
        */
        function checkSpecialKeys(e) {
            if (e.keyCode != 8 && e.keyCode != 46 && e.keyCode != 37 && e.keyCode != 38 && e.keyCode != 39 && e.keyCode != 40)
                return false;
            else
                return true;
        }
    </script>
</head>
<body>
    <div class="wrapper">
        <h1>Displaying The Character Count of a Textarea</h1>
        <textarea name="the-textarea" id="textarea" rows="4" cols="30" maxlength="250" placeholder="Start Typin..." class="char-textarea"></textarea>
        <div id="the-count">
            <span id="current">0</span>
            <span id="maximum">/ 250</span>
        </div>
    </div>
</body>
</html>

No comments:

Post a Comment

Creating Provider hosted app (sharepoint online) with local hosted IIS

The Pre-requires are as follows. 1. Office 365 Subscription 2. Visual Studio 2015 (Professional/Community/Enterprise Edition) With t...