Accessing elements in your HTML can be done with several DOM methods: getElementById, getElementsByTagName, getElementsByClassName, and querySelectorAll.
NOTE: In all of the methods which return a collection of resulting elements, the elements will be sorted in the order which they appear on the page (in the HTML), so the button order would be as follows:
let order = [
"top-left button", "top-right button",
"bottom-left button", "bottom-right button"
];
If the resulting button elements from the template are placed, they'll just have id values of post-1 for template-0 and post-2 for template-1. With two columns and two rows of buttons, this means that there will only be two unique IDs between four buttons, so selecting elements this way may not work well. The HTML code could probably be modified to be sure that the buttons have unique ID attributes. If that problem can be solved, the following code will select one button at a time:
let buttonElement = document.getElementById("post-1");
// or if ID can be resolved to be post-0-1:
let firstButtonElement = document.getElementById("post-0-1");
Using this method would work fine as it will return a list of button elements as an HTMLCollection. It may return other buttons on the page, but it's a minor inconvenience that can be handled.
let buttonsCollection = document.getElementsByTagName("button");
If you use a class for the buttons instead of IDs, the buttons can all be assigned to the same class and this is one of the ways it will select all of the buttons as intended and none of the buttons you didn't intend.
<template id="template-0">
<button id="post-1" class="postButtons">Item 1</button>
</template>
<template id="template-1">
<button id="post-2" class="postButtons">Item 2</button>
</template>
let postButtons = document.getElementsByClassName("postButtons");
console.log(postButtons);
This method combines multiple selection types into one (using CSS selectors):
| Description |
Method call |
| Tag names |
document.querySelectorAll("button"); |
| Class names |
document.querySelectorAll(".postButtons"); |
| Id |
document.querySelectorAll("#post-1"); |
Since you can use CSS Selectors, you could select buttons with id values that start with post- like so:
let postButtons = document.querySelectorAll("button[id^=post-]");
I'm not entirely sure how the elements will be created as for the CH5 library, but these steps provided above should help you get started.
Code Snippet showing all of the above examples in one:
let buttonElementPostOne = document.getElementById('post-1');
console.log("getElementById:".padStart(23, " "), buttonElementPostOne);
let buttonsCollection = document.getElementsByTagName('button');
console.log("getElementsByTagName:".padStart(23, " "), buttonsCollection[0]);
let postButtons = document.getElementsByClassName('postButtons');
console.log("getElementsByClassName:".padStart(23, " "), postButtons[0]);
let postButtonsQueried = document.querySelectorAll('button[id^=post-');
console.log("querySelectorAll:".padStart(23, " "), postButtonsQueried[0]);
<div>
<button id="post-1" class="postButtons">post-1</button>
<button id="post-2" class="postButtons">post-2</button>
</div>
<div>
<button id="post-3" class="postButtons">post-3</button>
<button id="post-4" class="postButtons">post-4</button>
</div>