I can't seem to get any word counting in the code. I start typing but there's no keyboard event triggered. Any ideas?
HTML Code:<form action="#" method="post" id="theForm"> <div> <label for="comments">Comments</label> <textarea name="comments" id="comments" maxlength="100" required></textarea> </div> <div> <label for="count">Character Counts</label> <input type="text" name="count" id="count" disabled/> </div> <script src="text.js"></script> </form>Code:function $(id) { 'use strict'; if (typeof id == 'string') { return document.getElementById(id); } } function addEvent(obj, type, fn) { 'use strict'; if (obj && obj.addEventListener) { // W3C obj.addEventListener(type, fn, false); } else if (obj && obj.attachEvent) { // Older IE obj.attachEvent('on' + obj, fn); } } function limitText() { 'use strict'; var comments = $('comments'); var count = comments.value.length; $('count').value = count; if (count > 100) { comments.value = comments.value.slice(0, 100); } } window.onload = function () { 'use strict'; addEvent($('comments'), 'keyup', limitText); };


Reply With Quote





Bookmarks