I have a html table and I'm trying to use jJavascript to trigger an event whenever a row loses focus but the "blur" event doesn't seem to be right as nothing is firing:
(quick example of what I'm doing)
<tr class="tableRow">
<td class="tg-amwm" contentEditable="true">hours</td>
<td class="tg-amwm" contentEditable="true">minutes</td>
<td class="tg-amwm" contentEditable="true">hours</td>
<td class="tg-amwm" contentEditable="true">minutes</td>
</tr>
and I"m using the following:
var rows = document.getElementsByClassName("tableRow");
for(i = 0; i < rows.length; i++) {
rows[i].addEventListener("blur", function(){console.log("row left!");});
}
but nothing is apprearing in the console - am I misunderstanding the event/DOM structure?
The focusout event occurs when an element (or any elements inside it) loses focus. The focusout() method attaches a function to run when a focusout event occurs on the element, or any elements inside it. Unlike the blur() method, the focusout() method also triggers if any child elements lose focus.
The focusout event fires when an element has lost focus, after the blur event. The two events differ in that focusout bubbles, while blur does not. The opposite of focusout is the focusin event, which fires when the element has received focus.
The onfocusout event occurs when an element is about to lose focus. Tip: The onfocusout event is similar to the onblur event. The main difference is that the onblur event does not bubble. Therefore, if you want to find out whether an element or its child loses focus, you should use the onfocusout event.
The opposite of focus is the blur event, which fires when the element has lost focus.
The row probably never receives focus, the cells in it do.
Unfortunately, blur
doesn't bubble. But if you hook blur
on each cell, then click one of those cells to give it focus, then click something else to take focus away, it should work:
var cells = document.querySelectorAll(".tableRow td");
for (var i = 0; i < cells.length; i++) {
cells[i].addEventListener("blur", handler);
}
function handler() {
console.log("row left!");
}
<p>Click a cell below to give it focus</p>
<table>
<tbody>
<tr class="tableRow">
<td class="tg-amwm" contenteditable>hours</td>
<td class="tg-amwm" contenteditable>minutes</td>
<td class="tg-amwm" contenteditable>hours</td>
<td class="tg-amwm" contenteditable>minutes</td>
</tr>
</tbody>
</table>
<p>Click here to take focus away</p>
Alternately, use focusout
, which was originally an IE-only event but which has been added to Chrome but not, as far as I can tell, Firefox:
document.querySelector("table").addEventListener("focusout", function() {
console.log("Left!");
});
<p>Click a cell below to give it focus</p>
<table>
<tbody>
<tr class="tableRow">
<td class="tg-amwm" contenteditable>hours</td>
<td class="tg-amwm" contenteditable>minutes</td>
<td class="tg-amwm" contenteditable>hours</td>
<td class="tg-amwm" contenteditable>minutes</td>
</tr>
</tbody>
</table>
<p>Click here to take focus away</p>
Side note for jQuery users: jQuery makes focus
and blur
bubble even though they don't natively, so you could use event delegation for the above with jQuery.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With