jquery - Javascript function to add to or splice array -


i have 2 buttons, each button adds array array orderarray. works fine , array displayed html table. when table output button created. buttons purpose remove array associated it, , hence remove line table.

this works fine, after removing part of array .splice not possible add more array, throws "cannot read property length".

you can see in console array spliced , length value correct error still persists. not getting here, thought loop calls myarray.length right length every time.

here js:

var orderarray = []; var ordernumber = 0; var theorder = []; var total = 0;  function orderupdate(item,price){     theorder = [item, price];     orderarray[ordernumber] = theorder;     ordernumber++; }  function maketable(myarray) {     var result = "<table border=2 id=ordertable>";     console.log(myarray.length);     for(var = 0; < myarray.length; i++) {         result += "<tr id='row" + + "'>";         for(var j = 0; j < myarray[i].length; j++){             result += "<td>" + myarray[i][j] + "</td>";          }         result += "<td><button onclick='removeline(" + + ")'>remove</button></td></tr>";     }     result += "</table>";     console.log(myarray);     return result; }  $( "#longb" ).click(function() {     orderupdate("long black", 2.50);     $("#ordered").html(maketable(orderarray)); });  $( "#flatw" ).click(function() {     orderupdate("flat white", 3.50);     $("#ordered").html(maketable(orderarray)); });  function removeline(arrayindex){     orderarray.splice(arrayindex, 1);     console.log(orderarray);     $("#ordered").html(maketable(orderarray)); } 

and html:

<html lang="en"> <head>     <meta charset="utf-8">     <title>jspos</title>     <script src="http://code.jquery.com/jquery-2.1.4.js"></script>  </head> <body>  <button id="longb">long black</button> <button id="flatw">flat white</button> <h3>ordered:</h3> <div id="ordered"></div>  <script src="js/stuff.js"></script> </body> </html> 

and here fiddle.

try substituting orderarray.push(theorder); orderarray[ordernumber] = theorder;

function orderupdate(item,price){     theorder = [item, price];     orderarray.push(theorder);     // ordernumber++; } 

jsfiddle https://jsfiddle.net/purnrntr/2/


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -