Multiple IDs in a single JavaScript click event
Asked Answered
G

7

26

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.

Garlicky answered 29/8, 2013 at 10:40 Comment(3)
use a class and the magic "this" :)Englebert
it would be much easier if pro will be an arraySoundless
yep use a class and the "i" woud be $(this).index();Anecdotage
S
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]
    });
});
Sombrous answered 29/8, 2013 at 10:48 Comment(2)
This answer doesn't make sense. .pros is a selector for a class called prosDiviner
@Diviner Your comment does not make sense. I had suggested using classes. This is why there is a class selector there.Sombrous
S
76

Try this:

var that = this;
$('#pro1,#pro2,#pro3').click(function () {
    chart.series[0].update({
        data: that[$(this).attr('id')];
    });
});
Suckling answered 29/8, 2013 at 10:47 Comment(1)
in 2021, var that = this; should just be replaced with an arrow functionDiviner
S
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]
    });
});
Sombrous answered 29/8, 2013 at 10:48 Comment(2)
This answer doesn't make sense. .pros is a selector for a class called prosDiviner
@Diviner Your comment does not make sense. I had suggested using classes. This is why there is a class selector there.Sombrous
G
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]
    });
});
Ginzburg answered 29/8, 2013 at 10:42 Comment(2)
proN is a variable not a string. If the variables are global window[this.id] will work.Sombrous
do you really think that OP wants to pass string as chart data?Soundless
A
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'

Ascent answered 29/8, 2013 at 10:42 Comment(2)
You had data: pro+i without defining vars for pro or i. Downvote removed.Birkner
Cheers, was a work in progress. Was actually in the middle of it.Ascent
D
2
$("*[id^=pro]").click(function () {
    chart.series[0].update({
         data: $(this).attr('id');
    });
});
Distorted answered 29/8, 2013 at 10:45 Comment(0)
F
0

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

Forespeak answered 29/8, 2013 at 10:43 Comment(0)
I
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
});

});});
Interstadial answered 22/9, 2020 at 5:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.