Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
164
rated 0 times [  166] [ 2]  / answers: 1 / hits: 23499  / 7 Years ago, tue, march 21, 2017, 12:00:00

I have multiple tables(with same structure)in my html. They are in tabs. I am creating tabs dynamically and i want to send those table data to the mysql database. So i wanted to get those data using javascript. I am correctly created TableData1,TableData2.... arrays using for loop. Problem is I cannot increment TableData here ('TableData'+i).shift(); . I am getting an error. I want to create TableData1.shift(),TableData2.shift().....



function myDataSendFunction(){
var i;

for(i = 1; i <= array_size; i++){
eval(var TableData+i+=[];);

$('#mytable'+i+ ' tr').each(function(row, tr){
('TableData'+i)[row]={
colum1 : $(tr).find('td:eq(1)').text()
, colum2 :$(tr).find('td:eq(2)').text()
, colum3 : $(tr).find('td:eq(3)').text()
, colum4 : $(tr).find('td:eq(4)').text()
, colum5 : $(tr).find('td:eq(5)').text()
, colum6 : $(tr).find('td:eq(6)').text()
, colum7 : $(tr).find('td:eq(7)').text()
, colum8 : $(tr).find('td:eq(8)').text()
}
});

('TableData'+i).shift();

}

}


I am getting this error.



Uncaught TypeError: (TableData + i).shift is not a function
at myDataSendFunction (<anonymous>:25:25)
at HTMLInputElement.onclick (create.php:1)

More From » jquery

 Answers
49

It's clear that you are very new to this, so I'm going to show you first and then explain second.



function myDataSendFunction(){
var i,
TableData = [];

for(i = 1; i <= array_size; i++){
TableData[i] = [];

$('#mytable' + i + ' tr').each(function(row, tr){
TableData[i][row]={
colum1 : $(tr).find('td:eq(1)').text()
, colum2 : $(tr).find('td:eq(2)').text()
, colum3 : $(tr).find('td:eq(3)').text()
, colum4 : $(tr).find('td:eq(4)').text()
, colum5 : $(tr).find('td:eq(5)').text()
, colum6 : $(tr).find('td:eq(6)').text()
, colum7 : $(tr).find('td:eq(7)').text()
, colum8 : $(tr).find('td:eq(8)').text()
};
});

TableData[i].shift();
}
}


Base on your comment:




what should i do then?? I tried with TableData array, then problem occurs in here TableData[i][row] with Uncaught TypeError: Cannot set property '0' of undefined




Your troubles revolve around setting properties on variables that haven't been defined. You must define TableData before attempting to assign a property.



Once TableData is initialized to an array (var TableData = []), you can set properties on TableData. But you cannot immediately set properties on properties of TableData. For example, you cannot jump directly to TableData[i][row]. You must first set TableData[i] to an array (TableData[i] = []), and then you can set TableData[i][row] to some value.



By trying to solve that problem with eval, you ran into a whole new world of problems. Try to avoid eval... it's a very complicated beast that tends to cause a lot of confusion and pain.



It may be helpful to review MDN's Working with objects documentation to better understand what's going on with JavaScript's array and objects.


[#58461] Saturday, March 18, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leslijessalyng

Total Points: 650
Total Questions: 85
Total Answers: 109

Location: Croatia
Member since Mon, Sep 6, 2021
3 Years ago
leslijessalyng questions
Fri, Feb 21, 20, 00:00, 4 Years ago
Tue, Jul 30, 19, 00:00, 5 Years ago
Fri, Jul 5, 19, 00:00, 5 Years ago
Wed, Mar 13, 19, 00:00, 5 Years ago
;