SunQuest
 
           JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old October 8th, 2003, 10:49 AM
js8 js8 is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 2 js8 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Moving Through Arrays Problem

Hi,

I am looking for a simple way of moving through an array. On my page I have a "View Next Tip" button that is supposed to display each element of the array one at a time each time the button is clicked. Here is the code I have right now:

<html>
<head>

<SCRIPT language="JavaScript">
<!--HIDE FROM INCOMPATIBLE BROWSERS


<!--This block of code is what I need help with

Array.prototype.next = function next_alert()

{

if (this.n == undefined || this.n == dog_tips.length)
{
this.n = 0;

}

return this[this.n++];
}

//End of block of code-->

var dog_tips = new Array()

dog_tips[0] = "a";
dog_tips[1] = "b";
dog_tips[2] = "c";
dog_tips[3] = "d";

onload = function next_alert() {window.alert(dog_tips.next())}



//STOP HIDING FROM INCOMPATIBLE BROWSERS-->
</SCRIPT>
</head>

This way works fine but I am looking for a simpler way of doing it. Is there any other way to display each element of an array one at a time or is the way I got it done the only way?

thanks,

js8

Reply With Quote
  #2  
Old October 8th, 2003, 11:12 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
I'd think something like this would be a little more straightforward. It also allows you to remove the alert from your onclick action. I haven't tested the code, but you get the idea.

Code:

dog_tips=new Array("a","b","c","d");
index=0;

function next_alert(){
    if(index>=dog_tips.length){
      index=0;
    }
    alert(dog_tips[index]);
    index++;
}

Reply With Quote
  #3  
Old October 8th, 2003, 11:49 AM
js8 js8 is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 2 js8 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thank you thank you thank you...it works perfectly

js8

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJavaScript Development > Moving Through Arrays Problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway