I am using a javascript plugin for gauges (http://bernii.github.io/gauge.js/).
The plugin works perfectly, but I can only make it work by using (getElementById) I want to be able to have several instances of this plugin on the same page (without repeating the code for each one), but all other javascript selectors (getElementByClass), (querySelectorAll), etc, return a list of NODE OBJECTS, instead of "ELEMENT OBJECTS" as the (getElementById) method does.
var opts ={
lines: 12,
angle: 0.15,
lineWidth: 0.44,
pointer: {
length: 0.9,
strokeWidth: 0.035,
color: '#000000'
},
limitMax: 'false',
colorStart: '#6FADCF',
colorStop: '#8FC0DA',
strokeColor: '#E0E0E0',
generateGradient: true
};
// this works
/*
var target = document.getElementById('gauge1');
var value = target.getAttribute('data-level');
var gauge = new Gauge(target).setOptions(opts);
gauge.maxValue = 1000;
gauge.animationSpeed = 32;
gauge.set(value);
*/
// this does not work
var target = document.getElementsByClassName('gauge');
for(var i=0; i<target.length; i++)
{
var value = target[i].getAttribute('data-level');
var gauge = new Gauge(target[i]).setOptions(opts);
gauge.maxValue = 1000;
gauge.animationSpeed = 32;
gauge.set(value);
}
<script src="http://bernii.github.io/gauge.js/dist/gauge.min.js"></script>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<canvas class='gauge' id="gauge1" data-level='900'></canvas>
<canvas class='gauge' id="gauge2" data-level='800'></canvas>
<canvas class='gauge' id="gauge3" data-level='700'></canvas>
</body>
</html>
So my question is: Is there a way to get an array of "ELEMENT OBJECTS" without using Id ?