I have tried solutions found in similar questions regarding Javascript, however, none of those solutions have worked. The script still continues to target ALL of the elements instead of just the last one. Below I have included my last attempt to get this to work. I have a chart with an id of "mainChart" and inside that chart, there is a "ct-series-b" element that contains "ct-bar" elements. I need to target the last "ct-bar" element contained within "ct-series-b" each time the script is run. How can I do this?
I have tried all of these solutions: Shortest way to get last element by class name in javascript and at best, it still targets all of the "ct-bar" elements.
JS
const Chart = document.getElementById("mainChart");
const series = Chart.querySelectorAll('.ct-series-b > .ct-bar')
var last = Chart.querySelectorAll('.ct-series-b > .ct-bar:nth-last-of-type(1)')
You can use the
:last-childselector. Note that this will get the last.ct-barunder each.ct-series-b, so if you only wanted it under one of them you could adjust the query.