Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect paragraph element change with JQuery

Is it possible to detect if the content of a paragraph has been changed in JQuery ?

I tried the below code.

<p id="test">Text</p>
<button id="submit1">change</button>

-

$(document).on("click", "#submit1", function () {
    var d = new Date();
    var time = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
    $("#test").text(time);
});       
$(document).on("change", "#test", function () {
    alert("Paragraph changed");
});

JSFiddle : http://jsfiddle.net/nnbqye55/

I guess I am missing something obvious.

like image 754
Soumya Avatar asked Dec 18 '14 07:12

Soumya


People also ask

How to change text inside an element using jQuery?

To change text inside an element using jQuery, use the text () method. You can try to run the following code to replace text inside an element:

How to select all the paragraph elements using jQuery?

With the help of jQuery, select all the paragraph elements. Apply some CSS property to the <p> element to see the change. You may use the .css () method to apply CSS property.

How do I change the text on a click in jQuery?

Using the html() Method to Change Text on Click The jQuery html()method is very useful when it comes to manipulating web pages. We can use the jQuery html()method to change the html and text of an element. Let’s say we have the following code and we want to add html content to the span inside of our paragraph.

How to replace the text of an empty string using jQuery?

Of course, you can use this same method to replace any text for an empty string removing the searched string from our text. We can replace the whole text of any element on our page by using the text function of jQuery. It's even more simple yet: $("#element").text("What's up!");


1 Answers

You can use 'DOMSubtreeModified' to check the dom changes on html tag elements. See the support for this event across browsers.

http://jsfiddle.net/nnbqye55/7/

$(document).on("DOMSubtreeModified", "#test", function () {
    alert("Paragraph changed");
});
like image 148
Monie corleone Avatar answered Oct 12 '22 17:10

Monie corleone