Obtaining input value from a JavaScript function

Using the highcharts 4 JavaScript library allows for customizing chart buttons.

Check out a chart with custom buttons

To customize buttons in highcharts, you can push button objects into an array and include this array in the Highcharts configuration.

However, there seems to be an issue when accessing parameters attached to each function upon clicking the button.

The snippet of code below demonstrates how to create the array of buttons:

for (var i = 0; i < arr_papers.length; i++) {
    var caption = arr_papers[i].caption;
    var colid = arr_papers[i].colid;

    buttons.push({
        text: caption,
        id: colid,
        onclick: function () {
            loadPerformanceBarChart(colid, caption);
        }
    });
}

This array is then included in the Highcharts configuration like so:

var options = {
    chart: {
        type: 'bar',
        renderTo: 'bar_chart_stats'
    },
    title: {
        text: 'Number of Students per Range'
    },
    exporting: {
        buttons: {
            contextButton: {
                menuItems: buttons
            }
        }
    },
.
.
.
.

One problem arises when calling the loadPerformanceBarChart function as the parameters colid and caption are always the last ones from the array. How to go around this?

Your insights are appreciated! Thank you.

Answer №1

It is recommended to use let or const for declaring variables within block scopes.

const title = arr_papers[i].title, year = arr_papers[i].year;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Use ajax request to serialize input into HTML value

I implemented a jquery.ajax post request to send data to the server using C#, however, I encountered an error with my code. Here is the code snippet: $(function(){ $('#frmSubmit').on('submit', function(e){ e.preventDefault(); ...

Issue with Highcharts failing to render points on regular intervals

I am facing an issue where the line graph in my frontend application using highcharts disappears or fails to draw to the next new data point every 30 seconds. Can anyone provide insight into why this might be happening? Check out the fiddle: http://jsfidd ...

Attempting to decode the string prior to selecting an item from the Vue.js/Nuxt array

Hey there, I really appreciate anyone who can assist me with this. I've been dabbling in Laravel for a few months and now I'm trying to dive into studying Nuxt. The specific type of translation I need help with is proving to be quite challenging ...

Disabling Firebase error logging for unsuccessful signInWithEmailAndPassword attempts

My current project involves setting up a login system using Firebase auth in NextJS. Strangely, when I call the login function with incorrect credentials, an error is logged to the console even though my catch statement for handling errors is empty. Is the ...

Using object references in a C++ loop

Similar Question: C++ Object references in loop cycle In my current project, I am facing a challenge while trying to create multiple objects of the same type within a loop. I am then attempting to store a pointer to each specific object in a linked li ...

Tokenizing with jQuery UI

I have been following a tutorial that utilizes jQuery UI to generate Facebook tokens, as shown in this link: However, I am facing an issue where I need to pass two values through JSON: the ID and the NAME. The server-side script is structured as follows: ...

Change the name of a file to a name chosen by the user while uploading it to an

I'm a beginner when it comes to node.js and I'm facing an issue that I couldn't find a solution for despite looking into various related topics. My problem involves using Node.js on the server side to upload a file and rename it based on a ...

Exploring Vue.js integration with Elasticsearch API functions

I have a script written in Vue.js and I am looking to incorporate an Elasticsearch API method. // ./main.js var Vue = require('vue'); Vue.use(require('vue-resource')); import ES from './elasticsearch.js'; new Vue({ el ...

Is accessing an array by reference causing issues?

To provide a clearer explanation, here is a code snippet: private $ParseRuleMap = array(); public function __construct( $rules ) { foreach( $rules as $which=>$rule ) { $mapping = $rule->getMinimumMatchables(); foreach( $mapping ...

Creating an HTML table using a PHP array is a straightforward process that allows for

Is there a way to create an HTML table using a PHP Array? Below is the PHP code: function get_location_list() { global $connect; $query = "select location , SUBSTRING_INDEX(SUBSTRING_INDEX(location,'-',1),'-&apo ...

Unable to properly align divs vertically

Having trouble getting some divs to align properly, whether they are in an accordion or not. The current layout is not what I want: My goal is to have them vertically aligned at the top, despite varying lengths. Any help would be appreciated. I attempted ...

merging JavaScript objects with complex conditions

I am attempting to combine data from two http requests into a single object based on specific conditions. Take a look at the following objects: vehicles: [ { vId: 1, color: 'green', passengers: [ { name: 'Joe', ag ...

JavaScript Switch Open/Close Mobile Navigation Menu

I'm currently facing a challenge with a specific issue. I want to implement a toggle menu for mobile devices on a website that I'm developing. It's functioning smoothly, and you can see it in action on this CodePen. The JavaScript code for ...

Reactjs - Creating a video component with a placeholder that loads the video seamlessly

I created a Video component that utilizes the React.Suspense component to show a placeholder while the video is loading. However, I'm facing an issue where it seems like the functionality isn't working as intended. When I set the network to "slow ...

Disabling the loading of JavaScript on a Magento website

Seeking advice - I'm experiencing a delay on my website due to 3 unnecessary javascripts that are being loaded. Is there a way to prevent these scripts from loading or being searched for? Listed below is the website log where I am unable to locate jq ...

Making adjustments to a row in the free jqGrid is a breeze with the ability

Using free jqGrid 4.12.1, I aim to incorporate functionality for adding, editing, and deleting rows in the grid with server-side calls for each operation. Below is the implementation of editurl and 'actions' formatter, { name: "actions", wi ...

"PHP array generated from JSON will exhibit variances if there is only a single

My current project involves using the Australia Post postcode lookup API, which returns a JSON response. For example, when I search for postcode 3094, the API returns: Array ( [localities] => Array ( [locality] => Array ...

Receiving a reply from the axios function

Whenever I try to call the lookUpItem function from ItemSearch.vue, I always get an undefined response. Code snippet from ItemSearch.vue: <script setup lang="ts"> import { lookUpItem } from '../systemApi' async fu ...

Paste the results of a JavaScript function into an Excel spreadsheet (codenamed "assault")

I currently have a website that utilizes a JavaScript function to validate a text input with 5 characters. The HTML code for this functionality is as follows: <p class="form-control-static ret"> Input your Text: <input ty ...

When attempting to display the image file path using the JavaScript API Notification in Angular, there is a challenge in

Hello fellow developers, I am currently facing an issue with retrieving a local image from my assets folder and displaying it on a new Notification object generated by the Web Notification API. I have an image in my assets folder with a .jpeg extension. ...