27

In JavaScript I am using click event to change chart data. Below is a method for click event.

$('#pro1').click(function () {
            chart.series[0].update({
                data: pro1
            });
        });
        $('#pro2').click(function () {
            chart.series[0].update({
                data: pro2
            });
        });
        $('#pro3').click(function () {
            chart.series[0].update({
                data: pro3
            });
        });

I need to minify these three click events in one event, means I want to write one click event which handle the id's. some thing like below code.

$('#pro'+i).click(function () {
chart.series[0].update({
     data: pro+i
});
});


I don't know how to do it exactly. The above code is not correct, it is just my lack of knowledge of JavaScript.

3
  • 1
    use a class and the magic "this" :) Commented Aug 29, 2013 at 10:41
  • it would be much easier if pro will be an array Commented Aug 29, 2013 at 10:42
  • yep use a class and the "i" woud be $(this).index(); Commented Aug 29, 2013 at 10:44

7 Answers 7

77

Try this:

var that = this;
$('#pro1,#pro2,#pro3').click(function () {
    chart.series[0].update({
        data: that[$(this).attr('id')];
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

in 2021, var that = this; should just be replaced with an arrow function
13

I would suggest creating an object and selecting the elements using classes, id of the clicked element retrieves value of the corresponding property of the helper object:

var pros = {
   pro1: '...',
   pro2: '...'
};

$('.pros').click(function () {
    chart.series[0].update({
        data: pros[this.id]
    });
});

2 Comments

This answer doesn't make sense. .pros is a selector for a class called pros
@Liam Your comment does not make sense. I had suggested using classes. This is why there is a class selector there.
11
$('#pro1,#pro2,#pro3').click(function () {
    chart.series[0].update({
        data: $(this).attr('id');
    });
});

Updated code

$('#pro1,#pro2,#pro3').click(function () {
    chart.series[0].update({
        data: window[this.id]
    });
});

2 Comments

proN is a variable not a string. If the variables are global window[this.id] will work.
do you really think that OP wants to pass string as chart data?
4

Use a class.

$('.pro').click(function () {
 chart.series[0].update({
   data: $(this).attr('id');
 });
});

And then on each of the #pro1, #pro2, #pro3 elements add a class of 'pro'

2 Comments

You had data: pro+i without defining vars for pro or i. Downvote removed.
Cheers, was a work in progress. Was actually in the middle of it.
2
$("*[id^=pro]").click(function () {
    chart.series[0].update({
         data: $(this).attr('id');
    });
});

Comments

0

You could give all of your elements a class name and use the :eq() selector within jQuery.

Comments

0

Using each function() you can do it

var i =0;
$("#pro"+i+", #pro"+i+", #pro"+i+"").each(function(){
            $(this).on('click', function(){
chart.series[0].update({
     data: pro+i
});

});});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.