Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect content change event on a div

I am trying to make an editor everything working fine till now, but now I need to make a handler which could detect any change made in a div or any content edited in the div

<?php $row="testing content" ?>
<!-- my editor section-->
<div id="editor">
    <div id="options">
        <span><a id="iimg">Insert Image from gallery</a></span>
        <span>Upload image to gallery</span>
        <span><a id="iheading">Heading</a></span>
    </div>
    <div id="product_descriptioncontent" contenteditable="true"><?php echo $row; ?>
    </div><!-- viewable editor -->
    <input type="hidden" name="textareacontent" value="" id="textareacontent" >
    <!-- hidden field to submit values -->
</div>
<!-- end of editor section -->
<div id="imc"></div> <!-- image gallery loading section -->

<script>
$(document).ready(function(){

    $('#iheading').click(function(){
    $('#product_descriptioncontent').append('<h1>Heading</h1>');
    });

    $('#iimg').click(function(){
        $('#imc').load('imagegallery.php',function(){
        $('#gallery img').on('click',function(){
            $('#product_descriptioncontent').append('<img  src="http://localhost/sites/site_pics/otherpic/1.png"><br>&nbsp;');
    });
    });
    });
    $('#product_descriptioncontent').change(function(){
    alert("pppp");// how to capture this event
    });
});
</script>

I have placed a bit of my code at jsfiddle http://jsfiddle.net/bipin000/UJvxM/1/ thanks for your precious time

like image 907
Bipin Chandra Tripathi Avatar asked Apr 26 '12 06:04

Bipin Chandra Tripathi


3 Answers

Try adding a handler for DOMCharacterDataModified. Might be a cleaner solution.

like image 108
mihai Avatar answered Oct 16 '22 11:10

mihai


Do you like this? http://jsfiddle.net/Ralt/hyPQC/

document.getElementById( 't' ).onkeypress = function( e ) {
    var evt = e || window.event
    alert( String.fromCharCode( evt.which ) )
}​

It's not waiting for a change event, it's kind of pointless.

Instead, it's listening to the onkeypress event. Everytime a user changes the content of this div (by adding a character to it), it triggers the event.

You can also see how to get the character clicked (using String.fromCharCode( evt.which )).

PS: a full jQuery solution for your specific case would be this:

$( '#product_descriptioncontent' ).on( 'keypress', function() {
    $( '#your-hidden-input' ).val( $( this ).text() )
    // Or
    $( '#your-hidden-div' ).text( $( this ).text() )
} )
like image 29
Florian Margaine Avatar answered Oct 16 '22 11:10

Florian Margaine


You can bind a custom event to the div and trigger that event upon change

Demo in Stack Snippets and jsFiddle:

$(function() {

  $('#wrapper').bind("contentchange", function() {
    console.log("Captured content change event"); 
  });

  $("#btn").click(function() {
    $('#wrapper').html("new value").trigger("contentchange");
  });
  
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

<div id="wrapper"></div>
<input type="button" id="btn" value="click here">
like image 1
Alon Eitan Avatar answered Oct 16 '22 12:10

Alon Eitan