Search the Community
Showing results for tags 'multiple elements'.
-
I am going through chapter 10 and I have run into a problem with focus and blur events on multiple form elements. I have a function which adds multiple events and handlers to elements. function addPlaceholderHandlers(elem) { 'use strict'; elem.onfocus = setOpacity; elem.onkeydown = removePlaceholder; elem.onblur = addPlaceholder; } // end of addPlaceholderHandlers function Then I run the following code in window.onload window.onload = function() { 'use strict'; var elements = document.getElementById('caseEval').childNodes; for (var i = 0, count = elements.length; i < count; i++) { var element = elements[i]; addPlaceholderHandlers(element); } }; The onkeydown is the only code that actually runs, and I suspect that this is so because the focus and blur events do not bubble. The obvious solution is to use the addEventHandler and set the phase to true to "capture" the event, however, I am not aware that IE has an equivalent for phase. Aside from referencing each individual element, adding the event to each element, and then setting an event handler for each individual element, is there a better alternative for selecting multiple elements when using the focus and blur?or should I change my event handlers to something other then: function removePlaceholder(evt) { 'use strict'; // get reference to the event if (!evt) evt = window.evt; // element triggering the event var target = evt.target || evt.srcElement; if (target.value === target.defaultValue) { target.value = ''; } target.style.color = '#000'; } // end of removePlaceholder function I would just like to add that I absolutely love this book and the support in this forum. I have never done any programming until three or so weeks ago, and I didn't even know what HTML was just a few weeks prior to that. I feel that this book is so well written, concise in its examples, and relevant that it should be in every beginners and maybe some intermediate programmer's bookshelf. You have definitely gained a lifelong reader.
- 9 replies
-
- event handler
- multiple elements
-
(and 1 more)
Tagged with: