jQuery: Passing i++ to click(function(){})

How can I pass the value for i from my while to a function on click?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Title</title> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
<script type="text/javascript"> 
 
$(document).ready(function(){
 $('a').each( 
  function(){
   var i=0; while (i++ <4){ 
    if(this.href.indexOf('i'+i) > -1) { $(this).click(function(){alert('Change color of link '+i);$('#i'+i).css('color', '#ff0000');});} 
   }
 }); 
});

</script> 
 
</head> 
<body> 
<div>  
  <h1 id="i1">Item 1</h1> 
  <h1 id="i2">Item 2</h1> 
  <h1 id="i3">Item 3</h1> 
  <h1 id="i4">Item 4</h1> 
  <p> 
   <a href="#i1">Link 1</a> 
   <a href="#i2">Link 2</a> 
   <a href="#i3">Link 3</a> 
   <a href="#i4">Link 4</a> 
  </p> 
</body> 
</html>

Open in new window

LVL 16
hankknightAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mahomeCommented:
You do! pass the i to the click function. Your problem is that when click-event is called the variable i is 5 and the old values are overriden. So you have to find another way to solve. I attached a solution for your example. In common: You need to extract your information out of the HTML and not out of a JavaScript variable, because this variable changes.

$(document).ready(function(){
 $('a').each( 
  function(){
   var i=0; while (i++ < 4){ 
    if(this.href.indexOf('i'+i) > -1) { 
        $(this).click(function(){
            var current = this.href.substring(this.href.indexOf('#i') + 2);
            alert('Change color of link '+ current);
            $('#i'+i).css('color', '#ff0000');
        });
    } 
   }
 }); 
});

Open in new window

0
hieloCommented:
the .each() already passes an index to its function argument as the first parameter. You can use that instead of the while:
$(document).ready(function(){
	$('a').each(function(idx){
		$(this).bind('click',function(){
			var i=1+idx;
			alert('Change color of link '+i);
			$('#i'+i).css('color', '#ff0000');
		})
	})
});

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.