3 Replies - 407 Views - Last Post: 13 January 2013 - 06:03 PM

#1 redshine6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-January 13

Problem with Javascript - Moving Stacks/Arrays

Posted 13 January 2013 - 04:55 PM

I have 2 Arrays and a Variable. I am able to push an item into a variable, and then on a second click, add it to an Array. However, i have another button that moves an item from that Array, back into the Variable and move the variable into another Array. My code works on the first section, however my function does now load for the second, any help please?
It is this section that does not seem to work

<INPUTtype=buttonvalue="Back"onclick='txtPop.value = popBackStack();showStack(theList);pushForStack(curUrl);showStack2(theList2);'>



The whole code is below:

<HTML>
<HEAD>
<TITLE>Stacking up!</TITLE>
<script>




var backStack = new Array(); 
var forStack = new Array();
var curUrl = document.getElementById("txtPop");


function pushStack(newVal) {
   backStack.push(curUrl);
   curUrl = newVal;
}


function pushForStack(newVal) {
    CurUrl = newVal;
    forstack.push(curUrl);
}


function popBackStack() {
   var popVal = backStack.pop();  
   if (popVal == undefined)
      return "Nothing left!";
   else
   return popVal
}


function popForStack() {
   var popVal = forStack.pop();
   if (popVal == undefined)
      return "Enter a new URL";
   else
   return popVal;
}


function showStack(theSelect){
   theSelect.options.length = 0;
   for (var i = 0; i < backStack.length; i++){
      var theOption = new Option(backStack[i]);
      theSelect.options[theSelect.options.length] = theOption;
   }
}


function showStack2(theSelect){
   theSelect.options.length = 0;
   for (var i = 0; i < forStack.length; i++){
      var theOption = new Option(forStack[i]);
      theSelect.options[theSelect.options.length] = theOption;
   }
}
</SCRIPT>






</HEAD>
<BODY>
<FORM>
<table width="104%" height="364" border="5" cellpadding="3" cellspacing="3">
  <tr>
    <th width="30%" height="78" scope="col"><p>
    <INPUT type=button value="Back" onclick='txtPop.value = popBackStack();showStack(theList);pushForStack(curUrl);
    showStack2(theList2);'></p></th>
    <th width="46%" scope="col"><p>
  <center>
  <INPUT type=text name=txtPush>
  <INPUT type=button value="Push" onclick='pushStack(txtPush.value);txtPush.value="";txtPop.value = curUrl; showStack(theList);'>
  </center>
  </p></th>
    <th width="24%" scope="col"><p><INPUT type=button value="Forward" onclick="txtPop.value = popBackStack();showStack2(theList2);"></p></th>
  </tr>
  <tr>
    <td><p><center>
    <SELECT name="theList" size=12>
    </SELECT>
    </center></p></td>
    <td><p><center><INPUT type=textki name=txtPop size=25></center></p></td>
    <td><center>
    <SELECT name="theList2" size=12>
    </SELECT>
    </center></td>
  </tr>
</table>
</FORM>
<p>&nbsp;</p>
</BODY>
</HTML>



Can anyone explain where I am going wrong?
Thank you very much

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with Javascript - Moving Stacks/Arrays

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2885
  • View blog
  • Posts: 9,580
  • Joined: 12-December 12

Re: Problem with Javascript - Moving Stacks/Arrays

Posted 13 January 2013 - 05:33 PM

    CurUrl = newVal;
    forstack.push(curUrl);


Javascript is case-sensitive so it should be curUrl.
Was This Post Helpful? 1
  • +
  • -

#3 redshine6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-January 13

Re: Problem with Javascript - Moving Stacks/Arrays

Posted 13 January 2013 - 05:58 PM

Thank you, that helped a lot, annoying to miss such a simple issue, however this has just brought to my attention a second issue. The forStack will only take on the first value when I press Back. When i press back a second time it just repeats the same value.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2885
  • View blog
  • Posts: 9,580
  • Joined: 12-December 12

Re: Problem with Javascript - Moving Stacks/Arrays

Posted 13 January 2013 - 06:03 PM

View Postredshine6, on 13 January 2013 - 05:58 PM, said:

Thank you, that helped a lot, annoying to miss such a simple issue, however this has just brought to my attention a second issue. The forStack will only take on the first value when I press Back. When i press back a second time it just repeats the same value.


JS is case-sensitive :): forstack cf forStack.

This post has been edited by andrewsw: 13 January 2013 - 06:03 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1