When use onclick and when not

I am loading Javascript on my HTML page with Ids (div data-x="")(the right way) not contaminating HTML with Javascript like onclick etc. .

…after retrieving/loading with Javascript (a dynamic content inside the div tag), I can not use the id’s for Javascript anymore (inside the loaded content, because JS on ready state has been already loaded and the content was generated later), so I am using onclick now inside the “later loaded content”.

Is this the right approach to use onclick='func1();' or not, is there any other way at all.

Thank you

a click event listener attached to the body of the page and then work out what was clicked from within the function the click event listener calls

Do you mean like this, added to the dynamic content(that is loaded after the main page), once loading the content you can also activate this JavaScript aElistener (in your JavaScript file):

   document.getElementById('testi').addEventListener("click", function() {	 
        alert("hello");
      } );

Thank you

Edited the double click function

Note, the “Point”: You need to call the function you would like to use inside the other function that loaded(after loading) the new content. It’s quite logical but, nah missed it in this thread.

No - that is telling it to add a click listener when another click listener is triggered by a click.

The second time that is clicked both of the listeners will add another making 4 then 8 …

Sry… now should be ok…

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.