I read about unobstrusive JavaScript and how Javascript should be not explicitly placed inside HTML code. Fine! But I have these hundreds of search results where I placed click events in the HTML structure because the function is the same, but the arguments passed are totally different from one entry to the next. For example:
HTML:
<button id="button_details_0"
class="search_details__button"
onclick="get_details('002124482_2242.jpg','4','0','Brasil, São Paulo, Atibaia, Inventários e Testamentos, Parte A')">
<span id="search_details_span_0">˅</span>
</button>
Also, when I open the details of a particular entry, the user has the option to get more information. Then, I call another function with even more different options.
The easy way of doing this is to add onclick in the HTML when the page is generated by Perl CGI. If I want to be true to unobstrusive Javascript, what do I place in the HTML to pass all this information to Javascript besides creating a form for each entry?
I tried moving the Javascript out of the HTML, but the new created details have new buttons. The progression is an entry, for example, with barebone christening data, then you click on details and more info about parents, godparents, clergy will appear. Then each name of the parents, godparents, clergy can be clicked on to obtain even more data. The initial data is presented:
HTML:
<button id="search_details_button_12" class="search_details__button" data-image_number="58" data-record_number="6" data-row_number="12" data-locality="Brasil, São Paulo, Atibaia, São João Batista, Batismos 1719-1752">
<span id="search_details_span_12">˅</span>
</button>
Then I have this at the end of the HTML so I assure the page is loaded fully: JS:
> // When user clicks on details of search
> const search_details_buttons = document.querySelectorAll('[id^=search_details_button');
> console.log(search_details_buttons); for (let j = 0; j <
> search_details_buttons.length; j++) {
> search_details_buttons[j].addEventListener('click', e => {
> get_details(search_details_buttons[j]);
> });
}
Then in the details area (after it is clicked), I get these buttons to which I want to addEventListeners:
<button id="details_ascend_button_11_1_1" class="details_ascend__button" data-row="11" data-role="1" data-individual="1">
<span class="details_ascend__span">↑</span>
</button>
But these new buttons in details are not created when the page is loaded but after the user clicks on details.
If Arrow 1 is clicked after details shows then the innerHTML changes back to the "barebone" information and the new created buttons disappears until clicked details is clicked again.
Where do I check for the existence of these new buttons and add event handlers?
Thanks you!

One should follow the already given advice of utilizing both,
data-*global attributes and each its relateddatasetproperty.A less verbose implementation, than the ones already provided, would go with just a single
data-*attribute likedata-details-paramswhich serves as configuration for all of theget_detailsfunction's parameters and as selector for querying any of the OP's buttons.I addition, neither markup nor code rely on id's; the only thing necessary, is the reference of the triggering button.
Edit ... taking into account following of the OP's comment ...
As already mentioned, and especially for the OP's problem/requirements, the event-handling has to be changed to event-delegation exclusively.
Additionally one should store every fetched markup into a
WeakMapinstance. Thus, one has to make any button-related detail-query API-call exactly once.The above provided code example then might get refactored to something similar to the next provided solution ...