Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Web Technology by (20.3k points)

I want to run a function when a user edits the content of a div with contenteditable attribute. What's the equivalent of an onchange event?

I'm using jQuery so any solutions that uses jQuery are preferred. Thanks!

1 Answer

0 votes
by (40.7k points)

You can try using the code given below:

$('body').on('focus', '[contenteditable]', function() {

    const $this = $(this);

    $this.data('before', $this.html());

}).on('blur keyup paste input', '[contenteditable]', function() {

    const $this = $(this);

    if ($this.data('before') !== $this.html()) {

        $this.data('before', $this.html());

        $this.trigger('change');

    }

});

For the project, you can refer to this: https://github.com/balupton/html5edit

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...