I am trying to create an array from the content of a table. The content of the tables looks something like this:
CSolution 1:
Check this one. I changed var content= []; to var content= {};
$(document ).on ('click' , '#guardarBtn' , function (event ) {
var content= {};
$('.rowUpdate' ).each (function (i ) {
$(this ).find ('td' ).each (function (j, v ) {
if (j != 0 ) {
var input = $("input" , this ),
name = input.attr ("name" ).substring (0 , input.attr ("name" ).length ),
value = input.val ();
content[name] = value;
}
});
alert (JSON .stringify (content));
});
});
Copy <script src ="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js" > </script > <table > <tr class ="rowUpdate" > <td > Corredor Feed</td > <td > Id Corrdor
<input type ="text" value ="" class ="validate" name ="idcorreo" > </td > <td > Nombre
<input type ="text" value ="" class ="validate" name ="nombre" > </td > <td > Email
<input type ="text" value ="foo@bar.com" class ="validate" name ="email" > </td > <td > Empressa
<input type ="text" value ="" class ="validate" name ="Empressa" > </td > <td > Pagina Web
<input type ="text" value ="" class ="validate" name ="paginaWeb" > </td > <td > Telefono
<input type ="text" value ="" class ="validate" name ="telephon" > </td > <td > Cellular
<input type ="text" value ="" class ="validate" name ="cellular" /> </td > <td > <input type ="submit" id ="guardarBtn" value ="Save" name ="submitme" > </td > </tr > </table >
Copy Solution 2:
Try this (keep the HTML the same):
$(document ).on ('click' , '#guardarBtn' , function (event ) {
var rows = [];
$('.rowUpdate' ).each (function (i ) {
var row = {};
$(this ).find ('td' ).each (function (j, v ) {
if (j != 0 ) {
var input = $("input" , this ),
name = input.attr ("name" ).substring (0 , input.attr ("name" ).length ),
value = input.val ();
row[name] = value;
}
rows.push (row);
});
});
Copy Solution 3:
Shorter way to access :
$(document ).on ('click' , '#guardarBtn' , function (event ) {
var content = {};
$('.rowUpdate' ).each (function (i ) {
$(this ).find ('input[type=text]' ).each (function ( ){
content[$(this ).attr ("name" )] = $(this ).val ();
});
});
alert (JSON .stringify (content));
});
Copy <script src ="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js" > </script > <table > <tr class ="rowUpdate" > <td > Corredor Feed</td > <td > Id Corrdor
<input type ="text" value ="" class ="validate" name ="idcorreo" > </td > <td > Nombre
<input type ="text" value ="" class ="validate" name ="nombre" > </td > <td > Email
<input type ="text" value ="foo@bar.com" class ="validate" name ="email" > </td > <td > Empressa
<input type ="text" value ="" class ="validate" name ="Empressa" > </td > <td > Pagina Web
<input type ="text" value ="" class ="validate" name ="paginaWeb" > </td > <td > Telefono
<input type ="text" value ="" class ="validate" name ="telephon" > </td > <td > Cellular
<input type ="text" value ="" class ="validate" name ="cellular" /> </td > <td > <input type ="submit" id ="guardarBtn" value ="Save" name ="submitme" > </td > </tr > </table >
Copy Solution 4:
Try This :
<form name ="form_name" id ='form_name' > <table > <tr class ="rowUpdate" > <td > Corredor Feed</td > <td > Id Corrdor
<input type ="text" value ="" class ="validate" name ="idcorreo" > </td > <td > Nombre
<input type ="text" value ="" class ="validate" name ="nombre" > </td > <td > Email
<input type ="text" value ="foo@bar.com" class ="validate" name ="email" > </td > <td > Empressa
<input type ="text" value ="" class ="validate" name ="Empressa" > </td > <td > Pagina Web
<input type ="text" value ="" class ="validate" name ="paginaWeb" > </td > <td > Telefono
<input type ="text" value ="" class ="validate" name ="telephon" > </td > <td > Cellular
<input type ="text" value ="" class ="validate" name ="cellular" /> </td > <td > <input type ="submit" id ="guardarBtn" value ="Save" name ="submitme" > </td > </tr > </table > </form >
Copy js Here :
$(document ).on ('click' , '#guardarBtn' , function (event ) {
var row=[];
$('.rowUpdate' ).each (function (i ) {
var content=[];
$(this ).find ('input' ).each (function (key,value ){
var field_name=$(this ).attr ('name' );
var field_value=$(this ).val ();
content[field_name]=field_value;
});
row.push (content);
});
console .log (row);
});
Copy Solution 5:
If I well understand the question, you just need for something like this (I'm not sure however why you used nested each
loops - is there any reason of this? Do you have more code in between these loops?)
$(document ).on ('click' , '#guardarBtn' , function (e ){
var content={};
$('.rowUpdate td' ).find ('input' ).each (function ( ){
content[this .name ] = this .value ;
});
alert (JSON .stringify (content));
});
Copy <script src ="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js" > </script > <table > <tr class ="rowUpdate" > <td > Corredor Feed</td > <td > Id Corrdor
<input type ="text" value ="" class ="validate" name ="idcorreo" > </td > <td > Nombre
<input type ="text" value ="" class ="validate" name ="nombre" > </td > <td > Email
<input type ="text" value ="foo@bar.com" class ="validate" name ="email" > </td > <td > Empressa
<input type ="text" value ="" class ="validate" name ="Empressa" > </td > <td > Pagina Web
<input type ="text" value ="" class ="validate" name ="paginaWeb" > </td > <td > Telefono
<input type ="text" value ="" class ="validate" name ="telephon" > </td > <td > Cellular
<input type ="text" value ="" class ="validate" name ="cellular" /> </td > <td > <input type ="submit" id ="guardarBtn" value ="Save" name ="submitme" > </td > </tr > </table >
Copy
Has A Class, Add Class To Table Cell
Let's say I have the following html: …
WebRTC Video Constraints Not Working
I'm trying to get a lower resolution from the webcam na…
HasClass Doesn't Work In My Js Code?
I want to use hasClass in the following code, but that does…
Capturing .click For URLs
I am relatively new to Javascript/Ajax. When the user click…
Getting Element By A Custom Attribute Using Javascript
I have an XHTML page where each HTML element has a unique c…
Logic For The Next Button For The Questionnaire?
I am beginner in AngularJS and facing some issues. I am try…
Payload Error In Jsonwebtoken
I am making a web application using nodejs and angular cli …
Post a Comment for "How To Save The Content Of Each Column Of A Row In A Jquery Array"