-1
I’m having trouble calling an event function when I dynamically rewrite HTML. I’m using pure javascript. Both functions are in a class file class tabela
, but when I click on the link that calls the function this.changePage('i')
she says there is no function. There is another way for me to declare this function next to HTML?
Below is the function that mounts the template:
templatePagination(data, numRegPerPage, page)
{
let numReg = data.length;
this.pages = Math.ceil(numReg/numRegPerPage);
let html = `
<nav aria-label="Page navigation example">
<ul class="pagination">
<li class="page-item">
<a class="page-link" href="#" aria-label="Previous" onclick="previousPage()">
<span aria-hidden="true">«</span>
<span class="sr-only">Previous</span>
</a>
</li>`;
for(let i=1; i<= this.pages; i++){
if(this.pages>1){
html += `<li class="page-item"><a class="page-link" onclick="${this.changePage('i')}">${i}</a></li>`;
}
}
html += `
<li class="page-item">
<a class="page-link" href="#" aria-label="Next" onclick="nextPage()">
<span aria-hidden="true">»</span>
<span class="sr-only">Next</span>
</a>
</li>
</ul>
</nav>`;
return html;
}
And the function that should be called:
changePage(pageSelected)
{
this.index = pageSelected;
this.updateTemplate();
}
I actually did something similar to your answer. I put the
addEventListener
after rendering the html:document.querySelector(
.page-item).addEventListener("click",()=>{
 this.changePage(page)
 })
. So it worked. Thank you.– Bruno