How can I blur/focusout a div where contentEditable=true? I have attempted $(elm).blur()
and $(elm).attr('contentEditable', false);
Answer: Use the HTML5 contenteditable Attribute You can set the HTML5 contenteditable attribute with the value true (i.e. contentEditable="true" ) to make an element editable in HTML, such as <div> or <p> element.
The contenteditable global attribute is an enumerated attribute indicating if the element should be editable by the user. If so, the browser modifies its widget to allow editing.
Just set contentEditable="false" . See this answer.
contenteditable is an HTML attribute that you can add to any HTML element. If its value is true or an empty string, that means that the user can edit it by clicking the element. For example: <div contenteditable="true"> Change me! </ div>
I would add another solution that is based on Omnicon's answer which is correct BTW. I have forked his fiddle and updated it.
The trick is to remove all ranges after calling blur:
$(".editable").blur();
window.getSelection().removeAllRanges();
$("div[contentEditable]").blur()
to blur all contentEditable elements.
$("#elementId").blur()
to blur an element by its id
.
The suggested solution by Roman Resh doesn't work when you click into the element and hit enter immediately after. It won't blur but create line-breaks/divs.
It is possible to prevent this behaviour entirely.
See this fiddle
$('<div class="temp-contenteditable-el" contenteditable="true"></div>')
.appendTo('body').focus().remove();
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