简体   繁体   中英

Execute javascript on html partial render in Ruby on Rails

I am trying to get some elements of a partial whenever it is rendered. The problem is that I can use document.addEventListener('DOMContentLoaded', () => {}); when the whole page is rendered but I am not able to figure out how to get the elements everytime the html partial is rendered.

js code

window.addEventListener('DOMContentLoaded', () => { 
  document.querySelectorAll('answer_choices');
});

How can I call this event whenever the partial is rendered. I can include the javascript file in the partial itself but it creates a lot of overhead. So this javascript file is included in the main html file but I want the querySelectorAll function to be triggered whenever the html partial is rendered. How can I do the same?

If you use Jquery - you can do it in that way:

$(function() {
   document.querySelectorAll('answer_choices');
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM