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

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 11-15-2012, 06:06 PM   PM User | #1
Md8^h2nx
New to the CF scene

 
Join Date: Nov 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Md8^h2nx is an unknown quantity at this point
for loop help

I'm working on a random project that takes a lot of code so I'm trying to use javascript to make it easier.

This is what I want to do.
Code:
else if(x.className=="change0") {
x.className="change1";
}
else if(x.className=="change1") {
x.className="change2";
}
else if(x.className=="change2") {
x.className="change3";
}

|
v
|
v
|
v

else if(x.className=="change358") {
x.className="change359";
}
I did a for loop for this.

Code:
<!DOCTYPE html>
<html>
<body onload="myFunction()">

<p id="demo"></p>

<script>
function myFunction()
{
var x="";
for (var i=0;i<359;i++)
  {
  x=x + "else if(x.className==&#34;change" + i + "&#34;) {<br>x.className=&#34;change#&#34;;<br>}<br>";
  }
document.getElementById("demo").innerHTML=x;
}
</script>

</body>
</html>
I can't get the first number to start at 0 while the other number starts at 1.

Code:
else if(x.className=="change0") {
x.className="change1";
}
Md8^h2nx is offline   Reply With Quote
Old 11-15-2012, 07:28 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Oh my, what a construct. Ever heard of variables holding numbers and string concatenation?

The only thing you want to do is to count +1 for the class name of a certain DOM element? Which DOM element is "x" referring to??
Code:
// get your element
var x = document.getElementById('yourelement');
// extract the number from the className
var current = Number(x.className.match(/(\d+)/)[1]);
// replace this number with +1 number
if(current<=358) x.className = x.className.replace(/\d+/, current+1);
devnull69 is offline   Reply With Quote
Old 11-15-2012, 07:28 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,249
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
That code just makes no sense at all.

As written, you will be simply dumping what LOOKS like JavaScript AS TEXT into the contents of your <div id="demo">.

Total nonsense.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant 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 09:19 AM.


Advertisement
Log in to turn off these ads.