Cesare Cesare - 1 year ago 145
HTML Question

how to know if a button has been clicked in javascript?

I need to know if the button to draw points on this map ...


is clicked.

I've tried to use this Javascript code ..

var editButton = document.getElementsByClassName('mapbox-gl-draw_ctrl-draw-btn mapbox-gl-draw_point'); // grab a reference to your element
editButton.onclick = function(){

... but nothing happens (no error ... ).

If I try to check for the editButton variable value in my Chrome console I can see that it's a HTMLCollection[1], so not empty ...

Suggestions / example / alternatives?

Answer Source

document.getElementsByClassName returns a collection. You need to set the click handler for the actual element.

var elements = document.getElementsByClassName('mapbox-gl-draw_ctrl-draw-btn mapbox-gl-draw_point');
var editButton = elements[0];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download