Mathieu Marques
Mathieu Marques

Reputation: 1748

How can I get a HTML attribute value from the click event handler in amCharts

I have added events listeners to my charts to catch clicks and perform actions.

chart.addListener('clickGraphItem', handleClick);

handleClick is a function in which the event instance is available, but I can't seem to be able to retrieve an attribute value I beforehand added to my HTML.

Any clue?

Upvotes: 0

Views: 1036

Answers (1)

martynasma
martynasma

Reputation: 8595

To get the value of the clicked item in click handler function, use the item property of the event object passed to it.

It's an instance of the GraphDataItem object, which has values property, which in turn is an object holding various related values for this data item, like value (which we're interested in), high, low, etc.

function handleClick(event) {
  alert(event.item.values.value);
}

If you want to retrieve the attribute of the chart HTML element container on chart click, you can use event.chart object. The chart object contains div property, which is a reference to the chart container. You can access any of it's properties that way:

function handleClick(event) {
  console.log(event.chart.div.id);
}

To retrieve a non-standard property of an HTML element, use its getAttribute() method. I.e.:

function handleClick(event) {
  console.log(event.chart.div.getAttribute("chart-id"));
}

Upvotes: 2

Related Questions