+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Div Deque?

  1. #1
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Div Deque?

    So I was trying to make something for a project and though I might need a deque for the project I was working on. Not knowing a ton about OOP inside of JS or how various things pass (reference or copy) etc I thought this would be a realistic approach for what I needed. When I got done I realized this would be a great tutorial since it was so easy to make.

    What is a deque? A deque is a double ended que. Most of you are familiar with it, but for those who are not, a stack is like a stack of plates. You put one plate on the top, and when you want the plate again you pick it up. First in last out. A deque is double ended so its kinda like those plates are hovering in the air, and you can put plates on the bottom and on the top and take one off from either end as well. If that was a horrible explanation click here.

    The idea behind this is having a div, and inside of that div I will use JS to move around there.

    So lets start off making a div, set the id to something we can remember "deque" seems appropriate to me. Add a input box and submit box to help us add things as well. I put an id on the text box as well since we will need the value.
    HTML Code:
    <input type="text" value="" id="insert">
    <input type="submit" value="Push!">
    <!-- Nodes here! -->
    <div id="deque"></div>
    To make this work should not be too hard, basically get the deque by the id, create a new div and add the content inside of it (innerHTML) then use javascripts "appendChild" to insert it into the bottom of the list.

    Code:
    <script>
        function 
    Push() {
            
    //declare variables
            
    var mydeque document.getElementById('deque'); //gets the deque
            
    var nodevalue document.getElementById('insert'); //gets the input box
            
    var mynode document.createElement('div'); //creates a div
            
            //insert
            
    mynode.innerHTML nodevalue.value//sets the value
                
    nodevalue.value ""//resets input box
            
    mydeque.appendChild(mynode);
        }
    </script>
    <input type="text" value="" id="insert">
    <input type="submit" onclick="Push();" value="Push!">
    <!-- Nodes here! -->
    <div id="deque"></div> 
    Now we need to be able to add from the top, so we going to basically do the same approach, add a dropdown so we know which side to add too, instead of appending were going to use "insertBefore" in javascript to add them.

    Code:
    <script>
        function 
    Push() {
            
    //declare variables
            
    var order document.getElementById('side'); //gets the oder 0 = first element, 1 = second
            
    var mydeque document.getElementById('deque'); //gets the deque
            
    var nodevalue document.getElementById('insert'); //gets the input box
            
    var mynode document.createElement('div'); //creates a div
            
            //insert
            
    mynode.innerHTML nodevalue.value//sets the value
                
    nodevalue.value ""//resets input box
            
    if(order.options.selectedIndex==0) {
                
    mydeque.insertBefore(mynodemydeque.firstChild);
            } else {
                
    mydeque.appendChild(mynode);
            }
        }
    </script>
    <input type="text" value="" id="insert">
        <select id="side">
            <option>Top</option>
            <option>Bottom</option>
        </select>
    <input type="submit" onclick="Push();" value="Push!">
    <!-- Nodes here! -->
    <div id="deque"></div> 
    Now to get values! This is really easy to do, were going to alert the value, and then remove the child using "removeChild" and with that use "firstChild" or "lastChild" so really simple.
    Code:
    <script>
        function 
    pop(id) {
            if(
    id==0) {
                
    //top
                
    var deque document.getElementById('deque');
                
    alert(deque.firstChild.innerHTML);
                
    deque.removeChild(deque.firstChild);
            } else {
                
    //bottom
                
    var deque document.getElementById('deque');
                
    alert(deque.lastChild.innerHTML);
                
    deque.removeChild(deque.lastChild);
            }
        }
    </script> 
    And the finished script, I added a little color to make it easier on the eyes!




    Code:
    <html>
    <
    head>
    <
    title>Div Deque!</title>
    <
    style>
        
    div.glow0 {
            
    border-left:4px solid #952fa4;
            
    padding0px 0px 0px 4px;
        }
        
    div.glow1 {
            
    border-left:4px solid #40af0e;
            
    padding0px 0px 0px 4px;
        }
        
    div.glow2 {
            
    border-left:4px solid #3d769e;
            
    padding0px 0px 0px 4px;
        }
    </
    style>
    <
    script>
        var 
    0;
        function 
    Push() {
            
    //declare variables
            
    var order document.getElementById('side');
            var 
    mydeque document.getElementById('deque');
            var 
    nodevalue document.getElementById('insert');
            var 
    mynode document.createElement('div');
            
            
    //insert
            
    mynode.setAttribute('class''glow'+ (i%3));
            
    mynode.innerHTML nodevalue.value;
                
    nodevalue.value "";
            if(
    order.options.selectedIndex==0) {
                
    mydeque.insertBefore(mynodemydeque.firstChild);
            } else {
                
    mydeque.appendChild(mynode);
            }
            
    i++;
        }
        function 
    pop(id) {
            if(
    id==0) {
                
    //top
                
    var deque document.getElementById('deque');
                
    alert(deque.firstChild.innerHTML);
                
    deque.removeChild(deque.firstChild);
            } else {
                
    //bottom
                
    var deque document.getElementById('deque');
                
    alert(deque.lastChild.innerHTML);
                
    deque.removeChild(deque.lastChild);
            }
        }
    </script>
    </head>
    <body>
    <a href="javascript:pop(0)">Pop from Top!</a> | <a href="javascript:pop(1)">Pop from Bottom!</a><br />
    <input type="text" value="" id="insert">
        <select id="side">
            <option>Top</option>
            <option>Bottom</option>
        </select>
    <input type="submit" onclick="Push();" value="Push!">
    <div id="deque"></div>
    </body>
    </html> 
    Last edited by BlaineSch; 11-11-2009 at 08:05 AM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Div Deque?

    Neat! Where can I see a working version?
    +rep

  4. #3
    Join Date
    Jul 2006
    Posts
    16,466
    Blog Entries
    74
    Rep Power
    143

    Re: Div Deque?

    Very neat. +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  5. #4
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: Div Deque?

    Yay it didn't get lost in the forums of time I was getting scared I would have to remake this lol.

    Working sample here.

  6. #5
    Join Date
    Aug 2009
    Location
    ~/
    Posts
    918
    Rep Power
    19

    Re: Div Deque?

    Very Nice!! +rep

  7. #6
    Jordan Guest

    Re: Div Deque?

    Very cool demo! The first time I press "Pop from bottom" I get undefined. After that, it works fine.

  8. #7
    Join Date
    Aug 2007
    Location
    Gizeh, Al Jizah, Egypt, Egypt
    Posts
    8,675
    Blog Entries
    12
    Rep Power
    81

    Re: Div Deque?

    love it
    +rep
    yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
    Code:
    eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
    www.amrosama.com | the unholy methods of javascript

  9. #8
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: Div Deque?

    Quote Originally Posted by Jordan View Post
    Very cool demo! The first time I press "Pop from bottom" I get undefined. After that, it works fine.
    Weird, for some reason the spacing inside the original div was used as a child? When I take it out it works fine.

  10. #9
    Join Date
    Sep 2009
    Location
    USA
    Posts
    3,400
    Blog Entries
    5
    Rep Power
    37

    Re: Div Deque?

    Root Beer == System Administrator's Beer
    Download the new operating system programming kit! (some assembly required)

  11. #10
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,851
    Blog Entries
    4
    Rep Power
    49

    Re: Div Deque?

    Cool man
    JS's yet another useful thing about it
    +rep.

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts