1

This is my html here i am taking elements from user and trying to update in js.

This my script file

  // Initializing the Stack Class
    
        function Stack() {
            this.dataStore = [];
            this.top = 0;
            this.push = push; // Inserting the element in Stack
            this.pop = pop; //Removing the element in Stack
            this.peek = peek;
            this.clear = clear;
            this.length = length;
        }
        
        // Adding an element in Stack
        function push(element) {
            this.dataStore[this.top++] = element;
        }
        
        function peek() {
            return this.dataStore[this.top - 1];
        
        }
        
        // Removing an element from the given stack
        function pop() {
            return this.dataStore[-this.top];
        }
        
        function clear() {
            this.top = 0;
        }
        
        function length() {
            return this.top;
        }
        
        var s = new Stack();
        
        function pushToStack(el){
            s.push(el);
        }
<!DOCTYPE html> 
  <html lang="en"> 
  <head> 
  <meta charset="UTF-8"> 
  <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
  <meta http-equiv="X-UA-Compatible" content="ie=edge"> 
  <title>Document</title> 
  </head> 
  <body> <!-- <div> <input type="text" id="stackName"> <button onclick="MakeStack()">Make a stack</button> </div> --> 
  <div> 
    <input type="text" id="elemet"> 
    <button onclick="pushToStack(document.getElementById('elemet').value)">
      Push an elemet
    </button> 
  </div> 
  <div class="container"></div>
  <script src="script.js"></script> 
  </body> 
  </html>

What i want is that when i click on push button the data from text filed should save in array as stack and data should show in container as my element of Stack. Thanks for help.

3 Answers 3

3

append the input to the Container, here is the code. Hope this is what you are looking for.

// Initializing the Stack Class

    function Stack() {
        this.dataStore = [];
        this.top = 0;
        this.push = push; // Inserting the element in Stack
        this.pop = pop; //Removing the element in Stack
        this.peek = peek;
        this.clear = clear;
        this.length = length;
    }

    // Adding an element in Stack
    function push(element) {
        this.dataStore[this.top++] = element;
    }

    function peek() {
        return this.dataStore[this.top - 1];

    }

    // Removing an element from the given stack
    function pop() {
        return this.dataStore[-this.top];
    }

    function clear() {
        this.top = 0;
    }

    function length() {
        return this.top;
    }

    var s = new Stack();
    function pushContainer(el){
      //console.log(el);
      var x = document.getElementById("container");
      x.appendChild(el);
    }
    function pushToStack(el){
      //
      var newElement = document.createElement("p");
      var Textnode = document.createTextNode(el);
      newElement.appendChild(Textnode);
      pushContainer(newElement);
      
      
      s.push(el);
    }
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  
</head>
<body>
 <div>
        <input type="text" id="elemet">
        <button onclick="pushToStack(document.getElementById('elemet').value)">Push an elemet</button>
    </div>
    <div id="container">

    </div>
</body>
</html>

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks ,but these error is showing on console Uncaught TypeError: Cannot read property 'appendChild' of null
but these error is showing on console Uncaught TypeError: Cannot read property 'appendChild' of null Which error are you taking about ? @PankajJaiswal
I have changed the container to id, if you want the container to be class just add the following code document.getElementsByClassName("container")[0].appendChild(el);
Thanks it worked , but there is error on console can you short it out , please !!
1

You can modify your push function as :

function pushToStack(el){
    s.push(el);
  var para = document.createElement("p");
  var node = document.createTextNode(el);
   para.appendChild(node);
   document.querySelector('.container').appendChild(para);
}

for working code, please refer:this pen

1 Comment

its working in pen.io but not working on my machine, even i pasted whole code
0

Modify your push function then:

function push(element) {
    this.dataStore[this.top++] = element;
    var div = document.createElement('div');
    div.textContent = element;
    document.getElementById('container').appendChild(div);
}

1 Comment

Corrected, didn't see you have an id instead of class on the div and due to you naming the variable element I thought that would be an HTMLElement already.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.