Krabat
01-24-2008, 01:33 PM
First off: I've searched the forum but since I'm not really sure what causes the problem I came back empty handed. Secondly, I spent 15 minutes trying to reformat my code until I realized that it might actually be not that helpful, so I'll try to explain the problem and post some code later if anyone wants to see it.
OK, my setup is approximatly as follows: I've got a set of items, broken up into different categories, each item is represented as a link, the categories are div tags. The user can select one of those items per category, so when he clicks another item in a category where there's already one selected, the previous selected one should deselect and the current one should switch to selected.
To do this every link calls a function called "clicked", to which it hands it's own category number, item number and the state it's currently in (0 for inactive, 1 for active and 2 for selected). If the state is "1" "clicked" knows the user wants to select the item, so it checks if there's a previously selected item in the category, saved in an array "cat".
if (cat[key] != -1)
If it's not "-1" it means there's an item selected, so "clicked" proceeds to deselecting it by calling a function called "changeState" which writes out the new link for the item (with a different call to "clicked"):
featureText = "<a href=\"#\" class=\"active\" onClick=\"clicked(" + key +"," + value + ",1)\">" + features[key][value][0] + "</a>";
It then replaces the innerHTML property of the List Item this item is saved in.
When that's done the function "clicked" proceeds to making the link the user just clicked the selected one, again using "changeState" and saving the item number in the "cat[]" Array.
Now inherently this all works, the user can always select only one item within each category. Unless one link is clicked multiple times in quick succession, or all the links in a category are clicked in quick succession. Then the script stops working insofar as now the user can select all the items within a given category.
I've placed an alert with some variables inside the "clicked" function to find out why that happened and saw to my surprise that the entry for the category in the "cat[]" array, which should either show "-1" if nothing is selected or the number of the selected item, became undefined when clicking the links very fast.
I'm quite at a loss here and would appreciate any input on whether this is a common Javascript problem (with common solutions) or whether this might be a problem within my code (in which case I would post the code later on to see if anyone can spot the error).
Thanks in advance!
OK, my setup is approximatly as follows: I've got a set of items, broken up into different categories, each item is represented as a link, the categories are div tags. The user can select one of those items per category, so when he clicks another item in a category where there's already one selected, the previous selected one should deselect and the current one should switch to selected.
To do this every link calls a function called "clicked", to which it hands it's own category number, item number and the state it's currently in (0 for inactive, 1 for active and 2 for selected). If the state is "1" "clicked" knows the user wants to select the item, so it checks if there's a previously selected item in the category, saved in an array "cat".
if (cat[key] != -1)
If it's not "-1" it means there's an item selected, so "clicked" proceeds to deselecting it by calling a function called "changeState" which writes out the new link for the item (with a different call to "clicked"):
featureText = "<a href=\"#\" class=\"active\" onClick=\"clicked(" + key +"," + value + ",1)\">" + features[key][value][0] + "</a>";
It then replaces the innerHTML property of the List Item this item is saved in.
When that's done the function "clicked" proceeds to making the link the user just clicked the selected one, again using "changeState" and saving the item number in the "cat[]" Array.
Now inherently this all works, the user can always select only one item within each category. Unless one link is clicked multiple times in quick succession, or all the links in a category are clicked in quick succession. Then the script stops working insofar as now the user can select all the items within a given category.
I've placed an alert with some variables inside the "clicked" function to find out why that happened and saw to my surprise that the entry for the category in the "cat[]" array, which should either show "-1" if nothing is selected or the number of the selected item, became undefined when clicking the links very fast.
I'm quite at a loss here and would appreciate any input on whether this is a common Javascript problem (with common solutions) or whether this might be a problem within my code (in which case I would post the code later on to see if anyone can spot the error).
Thanks in advance!