-1

I do shipping purchase. when user add to cart but not submit yet than user click to other page, I want store items in session.This is my code

    var tab=document.getElementById("shippingCartForm");
    var tbody = tab.getElementsByTagName( "tbody" )[ 0 ];
    var row1 = tbody.getElementsByTagName( "tr" )[ 0 ];
    var clnNode=row1.cloneNode(true);
    sessionStorage.setItem("name", clnNode); 

that is my html

    <table id="shippingCartForm"  border="1">
                <thead>
                   <th>No</th>
                   <th>Name</th>
                   <th>Price</th>
                   <th>Quantity</th>
                   <th>Remove From Cart</th>
             </thead>
             <tbody>
             </tbody>
         </table>

So when user click to other page or refresh page. I need chack session if it's has, it's should be show talbe shippingCartForm when user back to page purchase.

This is my script

    window.onload = function() {
        if (sessionStorage.getItem("name")!= null){
             var tableRef = document.getElementById('shippingCartForm');
             var newRow = tableRef.getElementsByTagName( "tbody" )[ 0 ];
             tableRef.appendChild(sessionStorage.getItem("name"));
        }
}

but I get message errors on console like this

[object HTMLTableRowElement] script.js (line 2)
TypeError: Argument 1 of Node.appendChild is not an object.
tableRef.appendChild(sessionStorage.getItem("name"));

please help me to resole it. thank for any idea.

** JavaScript only no Jquery

ching
  • 122
  • 7
  • 2
    Possible duplicate http://stackoverflow.com/questions/34362167/how-to-copy-all-row-in-table-to-store-in-session?noredirect=1#comment56466734_34362167 @ching is it the same thing? – pratikpawar Dec 18 '15 at 22:56
  • yes, it is, I re ask again – ching Dec 18 '15 at 22:59
  • You should continue on the same question. Make necessary edits to the same questions. This is something you should follow. http://stackoverflow.com/questions/18333427/how-to-insert-row-in-html-table-body-in-javascript – pratikpawar Dec 18 '15 at 23:00
  • oh,, wait, let me try – ching Dec 18 '15 at 23:05

1 Answers1

0

Replace

  tableRef.appendChild(sessionStorage.getItem("name"));

with

  newRow.appendChild(sessionStorage.getItem("name"));

tableRef is reference to table where as you are fetching reference to tbody in newRow variable. replacement will add row to tbody.

Also remove/delete duplicate question or question with less clarity out of 2 you asked on forum.

Try and debug the script see what values you are getting in each variable.

pratikpawar
  • 2,038
  • 2
  • 16
  • 20