Dynamic loading of modal window in WordPress

There is a block with modal windows, in which there are containers that are modal windows.
On the page there are buttons with data-path attribute containing data-target of modal window container.

Example:

<button data-target="order-1">Open order-1 Modal</button>

<button data-target="order-2">Open order-2 Modal</button>

<div class="modal">
    <div class="modal__container" data-target="order-1">
      content 
    </div>
    <div class="modal__container" data-target="order-2">
      content 
    </div>
</div>

By clicking on the button with the corresponding data-path of the modal window data-target, it is opened.

So, it is necessary to load the necessary modal window if there is a button on the WordPress page with the corresponding data-path attribute

Tried through js to check the entire page for the presence of buttons with data-path attribute and add to the body classes and in wp to perform the necessary operations, if get_body_class() has these classes, but this method does not work because js triggers later than php and in get_body_class() these classes do not appear.