score:7
This is a closure issue. The problem is here:
for (var index = 0; index<5; index++) {
items.push({text: "items "+index, onclick: function() {
alert(index);
}});
}
In each iteration, you are setting the value of onclick
to be a function which alerts index
. However, the index
variable within each of these functions are all bound to the one index
variable declared outside the function. And the value of index
that they will actually use by the time the function runs is the final value of index
, which is 5.
In order to fix this, you can use an IIFE (Immediately-Invoked Function Expression) wrapper to pass each value of index
into the anonymous function as a new variable i
whose value won’t change as index
changes.
for (var index = 0; index<5; index++) {
items.push({text: "items "+index, onclick: (function(i) {
return function(){
alert(i);
}
})(index)});
}
In other words, the wrapper function executes immediately and returns a new function which behaves like your original function except that it is not bound to the index
iterator.
Source: stackoverflow.com
Related Query
- Custom Highcharts Context Menu Button Appearing in Every Chart on Page
- Highcharts: Context Menu Button, onclick function
- Highcharts context menu button appearing thrice for the same chart
- Highcharts applying custom style to context button using css class
- HighCharts Hide specific context Menu items
- Highcharts context menu hidden behind series
- highcharts context menu hidden for small charts
- Highcharts context menu causes chart to disappear on return from print screen only on Chrome
- "viewFullscreen" menu item is not showing in the context menu of highcharts 6.2.0
- How do I align the hover/dropdown menu with the my custom context button symbol?
- Highcharts 3 like menu button won't display
- React highcharts unable to call function from data label onclick
- call to context menu function
- Highcharts CSS styling missing in exporting context button
- Highcharts CSS styling missing in exporting context button
- Set file type dynamically when click on exporting menu in highcharts using chart.exportChart function
- How to access function of buit-in button in highcharts using asp.net
- HighCharts Context Button
- Highcharts React: Menu button doesn´t change from "view full screen" to "exit full screen" when fullscreen mode is on
- Highcharts - not able to resize onclick a button in angular 6
- How to add and resize image in context button of highcharts
- How to remove button from Highcharts
- Highcharts :Uncaught TypeError: $(...).highcharts is not a function
- Highcharts saying undefined is not a function when trying to add a new chart
- Tooltip on custom button in Highcharts
- Highcharts chart click event fired on click of the reset zoom button (bug?)
- Highcharts : Chart with drilldown how to obtain click event of drill up button
- Execute function after zoom highcharts
- highcharts how to catch and insert logic in click reset zoom button event
- Enable or disable data labels shown in pie charts in Highcharts on click of a button
More Query from same tag
- HightChart graph lable cutoff issue
- Highcharts Error #18 in - Highcharts.Chart(options)
- How to disable color transition in highcharts gauge-solid
- vaadin's Invient chart max data size
- Printing table with charts in html through FireFox
- Highcharts: How to ignore styling from title string
- Highcharts conditional DrillDown not hiding DrillUp Button
- Highcharts x-axis label not corresponding with value in tooltip
- if i have a data in highcharts greater then 1000 i want to display 1k
- blank page in Highcharts
- Draw a vertical line when clicking on the series in highcharts.
- Highcharts: Displaying Linechart with missing datapoints
- highcharts-react Gantt: get mouse coordinates from mouse-over
- Highcharts: Bar column alignment in x-axis in uniform manner
- Pie chart slice radius using HighCharts library
- HeatMap or ColumnChart ideas for multi-category datetime data
- Highcharts : draw a chart with data from XMLHttpRequest ()
- UIWebview not scrolling in jquery part
- Highstock chart doesn't show after it has been cleared
- In highcharts how can I provide data with values x, y, title so that I can put the title in the tooltip?
- Preprocessing of dynamically added series in HighCharts
- highchart is not displaying in django twitter bootstrap
- Highchart : how to remove yAxis's path?
- highcharts custombutton hover and press changing colors by default
- ECharts how to set different symbol for different marklines in one chart
- Display marker or images inside bars of barchart in Highcharts
- Highcharts - Grouped Scatter? (relative to Grouped Column)
- HighCharts : Shared Tooltip with shared Data LABELS (in Series)
- Can I add custom data for highcharts RSI indicator?
- How to get id for every data point in highchart