Languages
[Edit]
EN

JavaScript - How to bind event on dynamically created elements?

7 points
Created by:
Kate_C
2857

In JavaScript it is possible to bind event on dynamically created element in following way.

1. Event listener in pure JavaScript (VanillaJS).

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <script>
  
    var element = document.createElement('button');

    element.addEventListener('click', function(event) {
      this.innerText = 'Button clicked!';
    });

    element.innerText = 'Click me!';

    document.body.appendChild(element);

  </script>
</body>
</html>

Note: as first argument for addEventListener method we can type any event from this list depending on what type of object we work.

Result:

Event bound on dynamically created element - before click operation.
Event bound on dynamically created element - before click operation.

 

Event bound on dynamically created element - after click operation.
Event bound on dynamically created element - after click operation.

2. element.on[eventname] property in pure JavaScript (VanillaJS). 

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <script>
  
    var element = document.createElement('button');

    element.onclick = function(event) {
      this.innerText = 'Button clicked!';
    };

    element.innerText = 'Click me!';

    document.body.appendChild(element);

  </script>
</body>
</html>

Note: in above code we can use as event property different combination of on + event name from this list depending on what type of object we work e.g. onmoveseover, onmouseout, ondblclick, etc.

Result:

Event bound on dynamically created element - before click operation.
Event bound on dynamically created element - before click operation.

 

Event bound on dynamically created element - after click operation.
Event bound on dynamically created element - after click operation.

3. jQuery on method example

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
  <script>
  
    var element = document.createElement('button');

    $(element).on('click', function(event) {
      this.innerText = 'Button clicked!';
    });

    element.innerText = 'Click me!';

    document.body.appendChild(element);

  </script>
</body>
</html>

Note: as first argument for jQuery.prototype.on method we can type any event from this list depending on what type of object we work.

Result:

Event bound on dynamically created element - before click operation.
Event bound on dynamically created element - before click operation.

 

Event bound on dynamically created element - after click operation.
Event bound on dynamically created element - after click operation.

Merged question

  1. JavaScript - How to add event on dynamically created element? 
Hey ūüĎč
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.