/    Sign up×
Community /Pin to ProfileBookmark

Break out of for-loop with a new variable

Hi, I have this piece of code:

[code]
for (var i=0; i < 6; i++)
{
$(‘a#chan’+i).click(function(){
for (var j=0; j<6; j++)
{
$(‘a#chan’+j).removeClass(“chSel”);
}
$(this).addClass (“chSel”);
channel = i;
});
}
[/code]

I know I use jQuery. The variable channel is declared this first thing in the Javascript using

[code]var channel = 0;[/code]

. I’m trying to put the current value of i in the variable channel but, everytime it loops, it stores 6. I tried using break; and still no luck; it goes to 6 anyways. I need help on this.

Thanks.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@alexdemersauthorFeb 22.2008 — I forgot to specify that I'm testing this with an unordered list:

[code=html]
<ul>
<li><a href="#" id="chan0">Primary Channel</a></li>
<li><a href="#" id="chan1">Channel 1</a></li>
<li><a href="#" id="chan2">Channel 2</a></li>
<li><a href="#" id="chan3">Channel 3</a></li>
<li><a href="#" id="chan4">Channel 4</a></li>
<li><a href="#" id="chan5">Channel 5</a></li>
</ul>
[/code]


And basically the effect on want to have is, each item is selectable and returns which one gets selected.
×

Success!

Help @alexdemers spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...