PDA

View Full Version : Would like some input for creating Next/Back Button for a random generated Tip JS



datawizard
Nov 25th, 2005, 07:08 PM
Hi all , I have created a JS Array that displays random generated tips on a HTML page. I would like to also let the user be able to click on either Next or Previous buttons and from the current displayed Tip be able to go forward or backwards in the array. the basic layout of my script goes like this
var Tips=new Array(31)
Tips[1] = "blah blah blah";
Tips[2]= "blah blah 2";
and so on

the functions of my script that actually displays the info is this(this works by the way)

//Declare variables
var T = Tips.length;
var whichTips=Math.round(Math.random()*(T-1));

//Set fucntion
function showTips(){document.write(Tips[whichTips]);}

now this part is where I am lost , this is where I've tried creating the next button,
function nextTips() {
showTips(){document.write(Tips[T + 1]);}
}
I figured the T(the variable T) + 1 would add one to the current tip that is displayed
The error message I get out of IE is the famous "Object Expected"
Any help for this project would be appreciated. :o Datawizard

pccode
Nov 25th, 2005, 09:30 PM
First make your variables global so that you can use them in any function.



//Declare global array
Tips=new Array(31);
Tips[0] = "blah blah blah";
Tips[1] = "blah blah blah";
Tips[2]= "blah blah 2";

//Declare global variables
T = Tips.length-1;
whichTips=Math.round(T*Math.random());

function nextTips() {
//if the whichTips variable equals the last variable in the Tips array, then start over at 0
if (whichTips == T) {
whichTips = 0;
document.write(Tips[whichTips]);
} else {
whichTips++;
document.write(Tips[whichTips]);
}
}

function prevTips() {
//if the whichTips variable equals the first variable in the Tips array, then start at the end
if (whichTips == 0) {
whichTips = T;
document.write(Tips[whichTips]);
} else {
whichTips--;
document.write(Tips[whichTips]);
}
}

datawizard
Nov 26th, 2005, 01:55 AM
Hi there,
I changed up the script and set
T = Tips.length-1;
whichTips=Math.round(T*Math.random()); to global variables
I tried the coding that is there for the next and prev functions but I get error of "Object expected" I read thru the lines to check for misspelling or missing operators but I'm not seeing anything out of place :confused: For something pretty simple here, I'm losing some hair , LOL Datawizard

pccode
Nov 26th, 2005, 08:38 AM
As far as I can tell, there's nothing wrong with the script. I've created a test page for you below. Just copy the whole script, paste it in notepad and test it in your browser. It should work fine.



<html>
<head>
<script>

Tips=new Array();
Tips[0] = "This is tip zero.";
Tips[1] = "This is tip one.";
Tips[2] = "This is tip two.";
Tips[3] = "This is tip three.";
Tips[4] = "This is tip four.";
Tips[5] = "This is tip five.";
Tips[6] = "This is tip six.";
Tips[7] = "This is tip seven.";
Tips[8] = "This is tip eight.";

T = Tips.length-1;
whichTips=Math.round(T*Math.random());

onload = function() {
document.getElementById('content').innerHTML = Tips[whichTips];
}

function nextTips() {
if (whichTips == T) {
whichTips = 0;
document.getElementById('content').innerHTML = Tips[whichTips];
} else {
whichTips++;
document.getElementById('content').innerHTML = Tips[whichTips];
}
}

function prevTips() {
if (whichTips == 0) {
whichTips = T;
document.getElementById('content').innerHTML = Tips[whichTips];
} else {
whichTips--;
document.getElementById('content').innerHTML = Tips[whichTips];
}
}

</script>
</head>
<body>
<span id="content">This is tip zero.</span>
<br><br>
<a href="javascript:nextTips()">next</a> &nbsp; <a href="javascript:prevTips()">prev</a>
</body>
</html>

datawizard
Nov 26th, 2005, 08:43 PM
Hi Pccode,
Thank you for your help, much appricated. When I compared the finished script you provided againt mine there were a few diffrences one major one that I noticed was I had "var Tips = new array(31)" and lines
"document.getElementById('content').innerHTML" where I had "document.write(Tips[whichTips]);" Im not sure if those were my problem areas as to getting the "object expected" error? The On load function was a nifty idea :thumbsup: so the good news is with those changes, the code works nicely, so thank you again for your help and input. Datawizard