views:

39

answers:

2

I have simple HTML Textbox with couple of events like "onfocus" change backgroud color, "onkeypress" making sure only accepting numerals, "onblur" formatting and validating the numerals to us-phone format, "onmouseover" showing the tooltip, "onmouseout" hiding the tooltip etc.

I want to manage all the events dynamically without using any 3rd party library (like prototype, jQuery,Dojo etc.). My browser is ONLY IE 6 and up. NO FIREFOX. I am expecting code under the <body> tag will be like <input type="text" id="usphone" name="usphone" title="enter phone with area code" class="usphoneClass"/> and code under <script> tag something like below.

document.getElementByID('usphone').onfocus= ChangeBackgroudColor();
document.getElementByID('usphone').onkeypress= return isNaN();
document.getElementByID('usphone').onblur= formatUSPhone();validateUsPhone();ChangeColor();
document.getElementByID('usphone').onmouseover= showtooltip();
document.getElementByID('usphone').mouseout= hidetooltip();
A: 

Although I don't agree with NOT using a third party library, if your tool tip has no links in it, you can try this

HTML

<a class="tooltipTarget" onclick="return false;">
     <div class="tooltip">BLAH BLAH BLAH yackity smackity</div>
</a>

CSS

a.tooltipTarget {position:relative;}
a.tooltipTarget .tooltip {display:none;}

a.tooltipTarget:hover .tooltip {
      position:absolute;
      right:10px; /* Adjust as per preference*/
      bottom:5px;
}
Zoidberg
A: 

IE has the attachEvent method to register event handlers. usage is

Object.attachEvent(eventName, method);

where eventName is a string in the form of onevent and method is a reference to a javascript function to call. the handler method will receive 1 parameter, which is the event object.

for your situation you would have

var phoneInput = document.getElementByID('usphone');
phoneInput.attachEvent('onfocus', ChangeBackgroudColor);  
phoneInput.attachEvent('onkeypress', function(e) { return isNaN(e.srcElement.Value); });
phoneInput.attachEvent('onblur', function (e) {
    formatUSPhone();
    validateUsPhone();
    ChangeColor();
});
// TODO onmouseover, onmouseout

note that the function references do not include parentheses because we're passing a reference rather than executing the function at this time. also if you register several handlers to the same element, the order of execution is not guaranteed. if you need functions to execute in a certain order like you indicate for onblur, you can wrap those calls in an anonymous function.

IE contains a reference to the element that fired the event in event.srcElement.

lincolnk