Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Focus / blur events on contenteditable elements

I'm trying to listen to focus / blur events on span with contenteditable=true attribute.

So here's what I tried (jQuery) :

$('.editable').on('focus',function(){
    var that = $(this),
        defaultTxt = that.data('default');
    that.html('');
});
$('.editable').on('blur',function(){
    var that = $(this),
        defaultTxt = that.data('default');
    if(that.html() === ''){
        that.html(defaultTxt);
    }
});

But he doesn't seem to work, because span doesn't handle focus / blur. How can I achieve that anyway (IE8 support needed) ?

like image 685
enguerranws Avatar asked Apr 10 '15 08:04

enguerranws


People also ask

What is the use of focus and blur events?

The blur event fires when an element has lost focus. The event does not bubble, but the related focusout event that follows does bubble. The opposite of blur is the focus event, which fires when the element has received focus. The blur event is not cancelable.

What kind of event occur when focus has left the Element?

The focusout event occurs when an element (or any elements inside it) loses focus.

Which of the following events get triggered with a control or element loses focus?

The onblur event gets fired when the user navigates to the input field and as soon as leaves the element i.e. the element goes out of focus for the user. The onblur event is the opposite of the onfocus event in which the event is triggered when the input field gets a focus.


2 Answers

There are two ways to achieve this effect.

Approach 1: focusin and focusout

$('.editable').on('focusin', function() {
    // your code here
});

$('.editable').on('focusout', function() {
    // your code here
});

focusin and focusout are like focus and blur events, but unlike the latter, they are fired on almost(?) every element, and also bubble. focusin and focusout are a part of DOM level 3 Specification. Firefox 51 and older didn't support this due to a known bug, but Firefox 52 and above have full support.

Approach 2: click and focus

This only works if you have other focusable elements around your span. What you do is basically use the focus event on any other element as a blur handler.

$('.editable').on('click', function() {
    // your code here
});

$('*').on('focus', function() {
    // handle blur here
    // your code here
});

I wouldn't recommend this approach in a large webapp, because browser performance will take a hit.

like image 102
turtleDev Avatar answered Sep 17 '22 23:09

turtleDev


I have created a demo for you:

$('.editable').bind('click', function(){
  $(this).attr('contentEditable',true);
});

$('.editable').bind('focus', function() {
  var that = $(this);
  //defaultTxt = that.data('default');
  that.html('');    
});

$('.editable').bind('blur', function() {
  var that = $(this);
  var defaultTxt = that.data('default');
  if(that.html() === ''){
    that.html(defaultTxt);
  }    
});
.editable{
    padding: 5px;
    border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<span class="editable" data-default="default">Some text</span>

I have changed your code, take a look it. Also now it's keeping the old value when lost the focus if you don't type anything.

like image 42
gon250 Avatar answered Sep 17 '22 23:09

gon250