1

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?

1 Answer 1

1

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

$(function() {
   document.querySelectorAll('answer_choices');
})
Sign up to request clarification or add additional context in comments.

3 Comments

Actually I have the task of migrating from jQuery to vanilla js. That's why I'm using vanilla js.
So you can use the source code of that Jquery function :) github.com/jquery/jquery/blob/…
Ok. Is there anyway to find out if a part of html is reloaded without reloading the whole page in vanilla javascript?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.