Languages
[Edit]
EN

JavaScript - How to bind event on dynamically created elements?

7 points
Created by:
Aleena
694

In JavaScript, it is possible to bind events on dynamically created elements in the 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? 
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join