3

I'm working on a website with Jquery tabs dynamically generated. Each tab has an ID.

For the purpose of my script I need to know how many times the user has clicked in a tab.

To record the number of clicks I was thinking of doing an array like this:

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

Where my_tab_id dynamically changes depending on the tab we're in. Sadly, it doesn't seem like the value of my_tab_id is translated into the array. I don't have i[5] = 0, i[6] = 0, etc. but rather i[my_tab_id], which doesn't help more than a simple var.

Any advice? Thanks!

Crescent Fresh
117k27 gold badges158 silver badges140 bronze badges
asked Jun 3, 2010 at 15:56
1
  • What you've shown is valid code. If it's not working, we'll need more context about how and wh en my_tab_id changes. Commented Jun 3, 2010 at 16:00

4 Answers 4

7

In that case you shouldn't use an array, you should use an object, which you can treat like a hash.

var o = {};
var id = 'x';
o[id] = 1;
alert(o[id]);
answered Jun 3, 2010 at 15:59
Sign up to request clarification or add additional context in comments.

Comments

1

This should allow you to store the click count onto each tab using the .data() function in jQuery each time a 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);
});
answered Jun 3, 2010 at 16:01

Comments

0

I think I understand your problem.
You are saying that the var i only has one element. This happens because the var i is newly instantiated every time you open a new tab. I'm guessing every tab is a new page or at least is a new context for var i.
If you want to keep an instance of an object (like an array) in between different pages, take a look at jStorage, it allows you to keep data locally on the browser and it makes it easier to maintain context between page loads.
If all tabs are on the same page then the solution is easier, you need to keep the array in a global variable for the page.

answered Jun 3, 2010 at 16:17

2 Comments

Yeah, exactly what's driving me crazy. Every time the user opens a new tab, it resets the var that stores the number of clicks. I've tried to make it global but i can't declare it cause I need to be in the function to have my_tab_id. I will definitely try jStorage.
If changing the tab does not generate a page refresh you can declare the variable i as global, outside the function and still use it inside the function. But if the tabs generate a page refresh you need to use something like jStorage to keep context between pages.
0

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

answered Jun 3, 2010 at 16:01

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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