Within my HTML table that contains multiple columns, I aim to create a toggle feature for one specific column (in this case, the 4th column). Currently, I have accomplished this using jQuery:
document.write('<a class="toggle" href="#!" data-alt="Hide">Show</a>');
$(document).ready(function() {
$('.toggle').click(function() {
var el = $(this);
var curr = el.text();
el.text(el.attr("data-alt"));
el.attr("data-alt", curr);
$('td:nth-child(4),th:nth-child(4)').toggle();
});
});
However, as this is the only part of my website that relies on jQuery, I would prefer to implement this functionality using pure Javascript. My attempts with getElementsByClassName
and classList.toggle()
weren't successful since I'm unable to add attributes to all td
and th
elements due to my Markdown-generated HTML structure.
I suspect that there might be a solution utilizing
document.querySelectorAll('td:nth-child(x)')
, but I haven't been able to crack it yet. Any suggestions?