On Oct 29, 7:10 am, Ozberk <[EMAIL PROTECTED]> wrote:
> Hello,
>
> This question is not actually related with Gadget Development directly
> although I'm using this code for a new gadget. It's rather JavaScript
> related. I've asked the same question in other groups/forums as well
> but haven't got any answers. So I'm trying my chances with my fellow
> Gadget Developers.
>
> I have written this jQuery code and manage to make it work:
>
> $('#item1').click(function() {
> if ($("#tabcontent1").is(":hidden")) {
> if(active == "init") {
> $("#tabcontent1").slideDown(300);
> active = "#tabcontent1";
> } else {
> $(active).slideUp(300, function() {
> $("#tabncontent1").slideDown(300);
> active = "#tabcontent1";
> });
> }
> } else {
> $("#tabcontent1").slideUp(300);
> }
>
> }
Could you use the slideToggle method here instead? Seems like it
could make this function much simpler.
>
> The problem has arisen after I decided to create a single for loop
> instead of binding all elements one by one.
Binding is exactly the problem, or lack thereof.
> what I really want is creating some kind of code like this:
>
> for(var i = 1; i < 6; i++) {
>
> var item = '#item'+i;
> var element = '#tabcontent'+i;
> $(item).click(function() {
> if ($(element).is(":hidden")) {
> if(active == "init") {
> $(element).slideDown(300);
> active = element;
> } else {
> $(active).slideUp(300, function() {
> $(element).slideDown(300);
> active = element;
> });
> }
> } else {
> $(element).slideUp(300);
> }
>
> }
> }
change your format from this:
for(var i = 1; i < 6; i++) {
var item = '#item'+i;
var element = '#tabcontent'+i;
// do stuff with element
}
to this:
for(var i = 1; i < 6; i++) {function(i) {
var item = '#item'+i;
var element = '#tabcontent'+i;
// do stuff with element
}(i)}
> The problem with the above code is the $(element) stays as the last
> assigned value (#tabcontent5).
Your trouble is with the variable i, which takes the value 5, since
all the click functions run after the loop has exited.
> I need to find a way to register that element
> variable as a static value instead of the variable itself.
Javascript binding: http://www.alistapart.com/articles/getoutbindingsituations
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"iGoogle Developer Forum" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/Google-Gadgets-API?hl=en
-~----------~----~----~----~------~----~------~--~---