Push form value to a stack in JavaScript

Description

The following code shows how to push form value to a stack.

Example


<!--   ww  w  .j  a v  a2  s.  c  o  m-->
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var stack = new Array();

function pushStack(newVal) {
stack.push(newVal);
}

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

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

</script>
</head>
<body>
<FORM>
<INPUT type=text name=txtPush>
<INPUT type=button value="Push" onClick='pushStack(txtPush.value);txtPush.value=""; showStack(theList);'>
<SELECT name="theList" size=12>
<OPTION>Displays the current state of the stack!
</SELECT>
<INPUT type=text name=txtPop size=25>
<INPUT type=button value="Pop" onClick="txtPop.value = popStack();showStack(theList);">
</FORM>
</body>
</html>

Click to view the demo

The code above generates the following result.

Push form value to a stack in JavaScript
Home »
  Javascript Tutorial »
    Data Type »
      Array
...
Delete an array element in JavaScript
Delete with Array splice() in JavaScript
Display Multi-dimensional array in a HTML t...
Do an alphabetical sort() method on strings...
Fill and populate two dimensional array in ...
Filter an array with a condition in JavaScr...
Get a string of the array elements separate...
Get array element value with index in JavaS...
Get the last element in an Array in JavaScr...
Insert with Array splice() in JavaScript
Integer Array Declaration with initializati...
Join array elements together with string se...
Loop through an array with for statement in...
Loop through array and work on each element...
Map array value in JavaScript
Move data from an Array to another in JavaS...
Output array with toString() in JavaScript
Output array with valueOf() in JavaScript
Push form value to a stack in JavaScript
Push two values to an empty array in JavaSc...
Reduce an array from the end with reduceRig...
Reduce array to a value in JavaScript
Reference array element by random index val...
Remove elements from array by changing the ...
Remove the first item in the array and retu...
Replace array element with Array splice() i...
Reverse an array in JavaScript
Search an array for an object with indexOf ...
Search an array from back at a specific end...
Search an array from start with indexOf met...
Search an array from starting index with in...
Search an array from the end for an object ...
Search an array from the end with lastIndex...
Set array element value with index in JavaS...
Slice an array to get a sub-array in JavaSc...
Slice array with negative value in JavaScri...
Sort Array Based on Argument Lengths in Jav...
Sort in descending order with custom method...
...