Execute custom Javascript after Drupal load Ajax successfully

Have you ever happened the custom Javascript that didn’t affect after load Drupal Ajax successfully? It is very confusing for all of the developers who are noobs in Drupal. But don’t worry about that. Here have three solutions that can be solved it.

Drupal behavior

It is provided API for the programmer by Drupal. You have to write the custom Javascript into Drupal behavior. When the Drupal Ajax is triggered, it will attach “Drupal behavior” to load the custom Javascript again. It is very useful. If you don’t know “Drupal behavior”, you can click this link that will explain how it work clearly.

Jquery on() event function

The Jquery official documentation suggests using “on of function” to create events. It has a lot of advantages. One of the advantages is getting dynamic elements easily.

Javascript getElementById

If you are using original Javascript to write the program, the getElementById function is your best friend. It also can get dynamic elements easily.

Conclusion

Drupal Ajax is very powerful for the developer. But most of the noob of developer are scared it. If you understand how it works, you will love it forever. And I prefer using “Drupal behavior” to fix ajax problems. Hope it can help you.