Category Archives: JavaScript

Dirty checking to warn for unsaved changes using jQuery

Copy following code snippet into the application. dirtyCount() method return the number of fields have unsaved changes. var dirtyChecking = function () { $(‘input, select, textarea’).each(function () { var ele = $(this); ele.attr(‘data-old’, ele.val()); // Look for changes in the value ele.on(“change keyup paste click”, function (event) { if (ele.attr(‘data-old’) != ele.val()) { ele.addClass(‘unsaved’); }…

Read More

Drag and drop from scratch using JavaScript

An example code to implement drag-ability in to an HTML element from scratch without using JavaScript library. JavaScript functions to provide drag-ability: function DDElement(ele, event) { addEvent(ele, “mousedown”, downHandler, true); addEvent(document, “mouseup”, upHandler, true); if (event.stopPropagation) event.stopPropagation(); else event.cancelBubble = true; if (event.preventDefault) event.preventDefault(); else event.returnValue = false; function upHandler(e) { e = e ||…

Read More

Cross browser event listener

Sometimes, we need to attach or detach an action with an element for specific event where listener function depends on browser. By using following code snippet anyone can do that in all browsers: function addEvent(obj, evType, fn, useCapture) { useCapture = (useCapture === undefined ? false : useCapture); if (obj.addEventListener) { obj.addEventListener(evType, fn, useCapture); }…

Read More