Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trigger an event when contenteditable is changed

When a divs value is changed, how Can I trigger an event?

<div class="changeable" contenteditable="true"> Click this div to edit it <div> 

So when its content changes I want to create an alert and/or do other things:

$('.changeable').text().change(function() {   alert('Handler for .change() called.'); }); 
like image 997
johnmosly Avatar asked Jun 06 '11 18:06

johnmosly


People also ask

How do you get the Contenteditable value react?

To listen for changes in a contentEditable element in React, we can listen to the input event on the div. to add a div with the contentEditable attribute set to true . Then we add the onInput prop and pass in a function to log the content of the div, which is stored in the e.

What does Contenteditable attribute do?

The contenteditable attribute specifies whether the content of an element is editable or not. Note: When the contenteditable attribute is not set on an element, the element will inherit it from its parent.

How do you use Contenteditable in JavaScript?

You can add the contenteditable="true" HTML attribute to the element (a <div> for example) that you want to be editable. If you're anticipating a user to only update a word or two within a paragraph, then you could make a <p> itself editable.

What does Contenteditable mean?

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.


1 Answers

Just store the contents to a variable and check if it is different after blur() event. If it is different, store the new contents.

var contents = $('.changeable').html(); $('.changeable').blur(function() {     if (contents!=$(this).html()){         alert('Handler for .change() called.');         contents = $(this).html();     } }); 

example: http://jsfiddle.net/niklasvh/a4QNB/

like image 61
Niklas Avatar answered Sep 23 '22 10:09

Niklas