Javascript array index is var

I am working on a site with dynamically generated jquery tabs. Each tab has an identifier.

For my script, I need to know how many times the user has visited the tab.

To record the number of clicks that I thought of creating an array as follows:

var i  = new Array(my_tab_id);
(...)
i[my_tab_id] = 0;

Where my_tab_id changes dynamically depending on the tab we are in. Unfortunately, it does not look like the value of my_tab_id has been translated into an array. I don’t have me [5] = 0, I [6] = 0, etc., But rather I [my_tab_id], which is nothing more than just var.

Any tips? Thank!

+3
source share
4 answers

, .
, var . , var i . , , , var i.
(, ) , jStorage, , .
, , .

0

, , .

var o = {};
var id = 'x';
o[id] = 1;
alert(o[id]);
+7

This will allow you to save the number of clicks on each tab using the function .data()in jQuery every time the tab is clicked.

$('#example').bind('tabsselect', function(event, ui) {
    var count = parseInt(ui.tab.data("clickCount"));
    if (isNaN(count)) count = 0;
    count++;
    ui.tab.data("clickCount", count);
});
+1
source

Are you sure it my_tab_idis an integer when called i[my_tab_id] = 0;?

0
source

Source: https://habr.com/ru/post/1748376/


All Articles