I've got a table on my wordpress, with a button on each row, which creates a small form when you click on it (on this page).
It's working perfectly fine, except when you change your browser's width to <768px, then nothing happens.
This is my main call :
var buttons = [];
if (jQuery(window).width() > 768)
buttons = document.querySelectorAll('td button');
else
buttons = document.querySelectorAll('div button');
/*
Add a click event to all buttons inside the table
*/
for (var i = 0; i < buttons.length; i++) {
buttons[i].addEventListener('click', clickCheck);
}
buttons
clickCheck()
function clickCheck(e) {
console.log('ok');
// if first click on the button
if (e.target.textContent === 'Inscription') {
// Cleaning an eventual earlier click on another button
for (var j = 0; j < buttons.length; j++) {
if (buttons[j].textContent === 'Fermer') {
buttons[j].parentNode.removeChild(buttons[j].parentNode.firstChild);
buttons[j].textContent = 'Inscription';
}
}
// Popping the form
formPop(e);
e.target.textContent = 'Fermer';
}
// if form submission
else {
// Depopping the form
formDepop(e);
e.target.textContent = 'Inscription';
}
}
buttons
addEventListener()
There's a lot of code running in your page so I don't know the root case, but here's the general problem.
When you do this at startup:
buttons = document.querySelectorAll('div button');
You are getting a different array of button elements than what ends up visible in your page. You can physically compare the original buttons
array with what happens when you run a new document.querySelectorAll('div button')
at the time of the click (I did so in the debugger). The first will contain 10 buttons, the second will contain 20 buttons. I think some code in your page is generating dynamic content AFTER you hook up the event handlers and what you're seeing and interacting with in the page is the dynamic content that was created after you ran your code to add click handlers so the visible content has no event handlers on it.