I want to get all the values of the inputs of each table in the body to later use it as json. I tried in various ways but only manage to read the first row of each table. I also tried with .serialize and .serializeArray but the result it complicates my reading a bit.
html code: https://jsfiddle.net/ogakn9mz/
jquery code:
function newattr(){
var attr_tag,opt_name,opt_price,opt_stock;
$('body').find('table').each(function() {
attr_tag = $(this).closest("div").find('input[name="attr_name"]').val();
opt_name = $(this).find('input[name="opt_name"]').val();
opt_price = $(this).find('input[name="opt_price"]').val();
opt_stock = $(this).find('input[name="opt_stock"]').val();
console.log(attr_tag+'/'+opt_name+'/'+opt_price+'/'+opt_stock);
});
}
Desired result:
{
"attributes": [
{
"attr_name": "Size",
"data":[
{
"opt_name": "Small",
"opt_price": "11",
"opt_stock":"22"
},
{
"opt_name": "Medium",
"opt_price": "33",
"opt_stock":"44"
}
]
},
{
"attr_name": "Color",
"data":[
{
"opt_name": "Blue",
"opt_price": "55",
"opt_stock":"66"
}
]
}
]
}