Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-21-2009, 03:22 AM   PM User | #1
hothousegraphix
Regular Coder

 
Join Date: May 2003
Location: Chicago, IL
Posts: 131
Thanks: 3
Thanked 0 Times in 0 Posts
hothousegraphix is an unknown quantity at this point
jQuery for loop with hover function

I'm trying to loop through my doc and alter the display of 5 containers on hover.

I can run my hover functions with no problem
Code:
$(document).ready(function() { 

    myDivs = '5';
    $('li').hover(

        function() {
            $('#div_1').addClass("hover");
		alert(myDivs);
        }, 

        function() {

            $('#div_1').removeClass("hover");

        }

    );
	
});
I can establish my for loop with no problem
Code:
$(document).ready(function() { 
    myDivs = '5';
    for(z=1;z<=myDivs;z++){
          alert(z);
    };
});
But when I attempt to combine - I get some odd behavior - I'm sure it's my syntax but I'm not sure exactly what it is.
Code:
$(document).ready(function() { 
	myDivs = '5';
	for(z=1;z<=myDivs;z++){
		alert('#div_'+ z);      // here z increments
		$('li').hover(
			function() {
				$('#div_'+ z).addClass("hover");
				alert('#div_'+ z);     // here z equal to 6 all the time ???
			}, 
			function() {
				$('#div_'+ z).removeClass("hover");
			}
		);
	}
});
any advice would be much appreciated.

Thanks

Last edited by hothousegraphix; 01-21-2009 at 03:42 AM..
hothousegraphix is offline   Reply With Quote
Old 01-21-2009, 05:53 PM   PM User | #2
hothousegraphix
Regular Coder

 
Join Date: May 2003
Location: Chicago, IL
Posts: 131
Thanks: 3
Thanked 0 Times in 0 Posts
hothousegraphix is an unknown quantity at this point
not sure if I'm making progress but it looks like I need to declare a variable within the for loop to represent my counter; like
Code:
$(document).ready(function() { 
    myDivs = '5';
    for(z=1;z<=myDivs;z++){
        count = z; // new counter variable
        alert('#div_'+ count); // variable increments
        $('li').hover(
            function() {
                alert('#div_'+ count); // variable now equal to 5 for every iteration ???
                $('#div_'+ count).addClass("hover");
            }, 
            function() {
                $('#div_'+ count).removeClass("hover");
            }
        );
    }
});
I'm still a bit lost as to why may variable is not increasing in value with "z".
hothousegraphix is offline   Reply With Quote
Old 01-23-2009, 03:03 PM   PM User | #3
bgallegos
New Coder

 
Join Date: Jul 2008
Posts: 45
Thanks: 0
Thanked 6 Times in 6 Posts
bgallegos is an unknown quantity at this point
Can you post how your html is structured? Thanks
bgallegos is offline   Reply With Quote
Old 11-26-2009, 05:35 PM   PM User | #4
podpi
New to the CF scene

 
Join Date: Nov 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
podpi is an unknown quantity at this point
exact same problem! javascript for loop problem with jquery

Code:

Javascript:
Code:
 for (k = 1; k <= 2; k++) {

        readMoreStr = "#readMore" + k;

        $(readMoreStr).click(function() {
            alert("Read more id: " + readMoreStr);
            // ALWAYS ALERTS "Read more id: 2" on both readmore divs when clicked
        });
    }
HTML:

Code:
<div class="readMore"  id="readMore1">
        <a href="" title="read more" class="readMoreLink">read more</a>
      </div>
<div class="readMore"  id="readMore2">
        <a href="" title="read more" class="readMoreLink">read more</a>
      </div>


any ideas guys?
thx

Last edited by podpi; 11-26-2009 at 05:56 PM..
podpi is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:50 AM.


Advertisement
Log in to turn off these ads.