Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Monday, July 8, 2013

If you remember I wrote how to limit input field only with numbers. I glad to show you that 6 years does not pass in vain and I find out better (primarily from UX point of view) way to do the same:
function stopEventPropagation(event) {
        if (event && event.stopPropagation) event.stopPropagation();
        if (window.event) window.event.cancelBubble = true;
    }
    function isNumberKey(evt){
        var charCode = (evt.which) ? evt.which : event.keyCode
        if (charCode > 31 && charCode != 46 && (charCode < 48 || charCode > 57))
            return false;
        return true;
    }
    <input type="text" onclick="stopEventPropagation(event)" onkeypress="return isNumberKey(event)"/>
    



Also I'd like to take this opportunity to mention that this approach is used on iRuler.net - ruler in actual size, so you can check it out there.

Friday, August 31, 2007

How get HTML element real position

I have spent a lot of time trying get the real position of DIV element. And I have done it, but only at Firefox. It is a complex issue to find real top and left of the element, because of IE and FF returns different offsetLeft and offsetTop and this result is depends on style.position value of element.
MochiKit helped me. There is an undocumented function MochiKit.Position.positionedOffset(element) that returns what I need. This function is not documented yet, because development still working. But as they says:"If it is denied but very, very need, than it is aloowed" :)
How I use it:

divForEntries.style.left =
    MochiKit.Position.positionedOffset(this.htmlView).x +
    this.htmlView.offsetWidth -5;
divForEntries.style.top = 
    MochiKit.Position.positionedOffset(this.htmlView).y;

Thursday, August 30, 2007

MochiKit - powerfull JavaScript framework



Many of us often have a deal with implementing base functions for JavaScript. At Java we have JDK with a lot of helpfull classes. But at the case of JavaScript developers often have to invent a wheel.
But I found a great framework - MochiKit. It is a set of util classes. I will not tell about all thier capabilities, only which I have used.

1. DIV slide up and slide down.

There is the function:
function slide(elementId){
  var element = document.getElementById(elementId);
  if (element){
    if (element.slideDown){
      MochiKit.Visual.slideDown(element);
      element.slideDown = false;
    }else{
      MochiKit.Visual.slideUp(element);
      element.slideDown = true;
    }
  }
}