PDA

View Full Version : HTML/JavaScript onfocus and onblur not firing



wolfgentleman
April 25th, 2013, 10:36 PM
I am trying to get the textboxes to clear on focus and if they are empty on unfocus they revert to the default values. However neither onfocus nor onblur is firing. I have a jsfiddle with the code here (http://jsfiddle.net/wolfgentleman/gTnSj/). Any help is appreciated.

wolfgentleman
April 27th, 2013, 10:24 PM
bump

epicoder
April 29th, 2013, 01:51 PM
Your focus event is not firing. However, your unfocus is. I'm not quite sure of the reason why, but you could probably save yourself some trouble by using the placeholder attribute. http://html5doctor.com/html5-forms-introduction-and-new-attributes/#placeholder
Also, consider using the addEventListener way of doing this, as the attributes are discouraged:


document.getElementById('ip1').addEventListener("focus", function () {
...
}, false);

wolfgentleman
April 29th, 2013, 06:24 PM
I could not get the onblur event to fire, I don't know what you are doing different... I tried the .addEventListener first, it didn't work. Then I used the placeholder attribute and it did work nicer than what I was trying to do. So thank you for your help.

BTW: if it makes any difference I am using Firefox, I don't know why that would be preventing the onblur to fire but whatever...