Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery event for html change in a div element?

Tags:

jquery

events

Long story short - I have an editable <div> and I want to clear formatting when someone pastes something in. Since jQuery has no control over the clipboard and I don't want to get into cross-browser compatibility, I figured I'd listen for an event that runs when the content changes.

I've tried $("#mydiv").change() but obviously that only works on text fields and textareas(?), so is there a way to do this?

I'd also accept alternative solutions, and any solution I choose to use will be marked as the correct answer.

Thanks!

like image 390
Hubro Avatar asked Sep 15 '10 23:09

Hubro


People also ask

Which jQuery function is used to change HTML of Div?

When . html() is used to set an element's content, any content that was in that element is completely replaced by the new content. Additionally, jQuery removes other constructs such as data and event handlers from child elements before replacing those elements with the new content.

How do you call a function inside a div?

You can name each function according to the div 's id and call it dynamically using the object["methodName"]() syntax to call it.

What is Onchange in jQuery?

The onchange event occurs when the value of an element has been changed. For radiobuttons and checkboxes, the onchange event occurs when the checked state has been changed.

How do I set the value of a element in jQuery?

jQuery val() Method The val() method returns or sets the value attribute of the selected elements. When used to return value: This method returns the value of the value attribute of the FIRST matched element.


1 Answers

You can make use of .keyup() for copy and pasting. The right click followed by choosing paste in the context menu doesn't seem to record a click, so .click() doesn't work.... instead use setInterval() to check every X seconds to capture right click pastes.

Not sure if you can bind .keyup() to the div (whether the div is focusable across all browsers), but all keyups bubble up to the document, so $(document) will always work.

$(function() {

      // Get initial text:
    var previous = $("#mydiv").text();

      // Make DIV editable if clicked
    $("#mydiv").click(function() { this.contentEditable = 'true'; });

      // Create a function for what to do if there is a change:
    $check = function() {

          // Check for a change
        if ($("#mydiv").text() != previous) {
            // What to do if there's been a change
            // ...
        }

          // Store what contents are for later comparison
        previous = $("#mydiv").text();        
    }

      // Add the div changed handler to both keyup (ctr + v)
      //   and mouseclick (right click paste)
    $(document).keyup($check);
      // Right click work around is to check every Xs
    setInterval(function() { $check(); }, 1000);
});​

jsFiddle Example


This works with pasting.... it captures ctr, shift, etc keys. (if you try it out w ctr-v and you release one key after the other, then keep an eye on the status, since the status will only show changed after the release of the first key and same after the release of the second.... as it should).


Note: I do like having both a .keyup() handler and a setInterval, since this guarantees that the feedback is instant for keystrokes.... even though there might be a lag after a right click paste.

like image 189
Peter Ajtai Avatar answered Sep 19 '22 15:09

Peter Ajtai