$(event.target) works on click, but not keydown... alternative?
I have this script working just fine when I click on it using the mouse, but it doesn't trigger the while() loop if I move to it using arrows:
- $('[data-ce]')
- .on('keydown click', function(e) {
- var node = $(e.target);
- while (node.is('contenteditable') !== 'true') {
- alert('node - ' + node[0].nodeName);
- alert('this - ' + this.nodeName);
- if (node[0].nodeName == 'B' && !$('[data-richcont="B"]').hasClass('richbuttonOn'))
- $('[data-richcont="B"]').removeClass('richbuttonOff').addClass('richbuttonOn');
- node = node.parentNode;
- }
- });
- })
This is within a contenteditable section, much like the reply field in this forum. When the user has bolded something, I'm changing the class for the "Bold" button to highlight.
I'm pretty sure that the problem is that when I key over to the bolded section manually, $(e.target) becomes equal to "this" instead of to the innermost node.
Is $(e.target) the wrong thing to use here? If not, can you guys suggest how to make it recognize properly when I key over to it?