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.'); });
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.
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.
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.
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 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/
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