Objective: Have multiple "Full Product Specifications" buttons that need to toggle the visibility of their respective Product Specification Tables. Initially, set all Product Specification tables to NOT display, then use the buttons to control. Cant use jQuery because ... reasons/stupid old website I don't have full control over...
I'm trying to use an anonymous JavaScript Function, but I'm unsure how to pass in my table? Or if I even can? My JS is not that great...
With jQuery I could probably just use .click() and .closest(). Unsure how to translate into pure JavaScript, so this is what I have instead.
The error...
Uncaught TypeError: Cannot set property 'display' of undefined at HTMLButtonElement.y.(anonymous function).onclick
And the code...
var x = document.getElementsByClassName("pdp-full-specs-table");
console.log("There are " + x.length + " tables");
for(i = 0; i < x.length; i++){
x[i].style.display = "none";
}
var y = document.getElementsByClassName("pdp-full-specs-toggle");
console.log("There are " + y.length + " toggles");
for(i = 0; i < y.length; i++){
var pdpTable = x[i];
y[i].onclick = function(pdpTable){
// Make this a toggle later
pdpTable.style.display = "table";
}
}
And the HTML in case that helps anyone
<button type="button" class="btn btn-link pdp-full-specs-toggle">Full Specifications ›</button><br>
<table class="pdp-full-specs-table">
<tr>
<td>Product Size</td>
<td>some size</td>
</tr>
</table>