|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
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++;
}
|
|
#3
|
|||
|
|||
|
Thank you thank you thank you...it works perfectly
![]() js8 |
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > Moving Through Arrays Problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|