call function on change of value inside <p> tag

b0y

Is there any way in JavaScript by which I can call a function when the value of a paragraph tag is changed.

Overview:

HTML:

<p id="timer">00:00</p>
<button onclick="change()">My Button</button>

JS:

function change() {
  document.getElementById("timer").innerHTML = "00:01";
}

function hello() {
  alert("Hello");
}

I want to alert("Hello") when the value of paragraph is changed. Something like a continuous function checking for a change in the value of paragraph.

guest271314

You can use MutationObserver with characterData option set to true

<script>
  function change() {
    document.getElementById("timer").innerHTML = "00:01";
  }

  function hello() {
    alert("Hello");
  }
  window.onload = function() {

    var target = document.querySelector("p");

    var observer = new MutationObserver(function(mutations) {
      mutations.forEach(function(mutation) {
        hello()
      });
    });

    var config = {
      childList: true,
      subtree: true,
      characterData: true
    };

    observer.observe(target, config);
  }
</script>

<p id="timer">00:00</p>
<button onclick="change()">My Button</button>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Call javascript function inside erb tag

From Dev

How to change value inside function

From Dev

Change the value of a variable inside a function

From Dev

Change outer variable value inside library call

From Dev

Dynamically add object value to div inside <p> tag

From Dev

Can I call a JavaScript function inside a <link> tag href?

From Dev

How to change the value of sum function inside the query?

From Dev

Change step value inside range function?

From Dev

Change p tag to input

From Dev

Change p tag to input

From Dev

I want to render my input text value inside <p></p> tag

From Dev

link inside a p tag in react

From Dev

How to get return value in a function with inside Ajax call - JQuery

From Dev

Node.js: Returning value from function with async call inside

From Dev

Node.js: Returning value from function with async call inside

From Dev

Synonym function call for ::new((void*)p)T(value);

From Dev

Change details of the tag inside the tag with jQuery

From Dev

Call div inside a function

From Dev

AFNetworking - call function inside

From Dev

Call a function inside promise

From Dev

How to change the value of data inside a struct through a function?

From Dev

How to change the value of data inside a struct through a function?

From Dev

How do I change the value of any global variable inside of a function?

From Dev

Change global variable value with a function from inside window.onload

From Dev

jQuery function to change td value with button inside td

From Dev

Can a variable change value during single function call (from the outside?)

From Dev

Why can a pointer to class can change value via function call?

From Dev

What is causing the value of this pointer to change from a simple function call?

From Dev

Call Function of inside function in Python

Related Related

  1. 1

    Call javascript function inside erb tag

  2. 2

    How to change value inside function

  3. 3

    Change the value of a variable inside a function

  4. 4

    Change outer variable value inside library call

  5. 5

    Dynamically add object value to div inside <p> tag

  6. 6

    Can I call a JavaScript function inside a <link> tag href?

  7. 7

    How to change the value of sum function inside the query?

  8. 8

    Change step value inside range function?

  9. 9

    Change p tag to input

  10. 10

    Change p tag to input

  11. 11

    I want to render my input text value inside <p></p> tag

  12. 12

    link inside a p tag in react

  13. 13

    How to get return value in a function with inside Ajax call - JQuery

  14. 14

    Node.js: Returning value from function with async call inside

  15. 15

    Node.js: Returning value from function with async call inside

  16. 16

    Synonym function call for ::new((void*)p)T(value);

  17. 17

    Change details of the tag inside the tag with jQuery

  18. 18

    Call div inside a function

  19. 19

    AFNetworking - call function inside

  20. 20

    Call a function inside promise

  21. 21

    How to change the value of data inside a struct through a function?

  22. 22

    How to change the value of data inside a struct through a function?

  23. 23

    How do I change the value of any global variable inside of a function?

  24. 24

    Change global variable value with a function from inside window.onload

  25. 25

    jQuery function to change td value with button inside td

  26. 26

    Can a variable change value during single function call (from the outside?)

  27. 27

    Why can a pointer to class can change value via function call?

  28. 28

    What is causing the value of this pointer to change from a simple function call?

  29. 29

    Call Function of inside function in Python

HotTag

Archive