-
-
Save pranid/3733d13be5360b231d0aaf4c9ffaad67 to your computer and use it in GitHub Desktop.
jQuery: Convert HTML Table to JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($){ | |
var convertTableToJson = function() | |
{ | |
var rows = []; | |
$('table tr').each(function(i, n){ | |
// Ignore empty | |
if(i != 0) { | |
var $row = $(n); | |
rows.push({ | |
display_name: $row.find('td:eq(0)').text(), | |
first_name: $row.find('td:eq(1)').text(), | |
last_name: $row.find('td:eq(2)').text(), | |
street: $row.find('td:eq(3)').text(), | |
city: $row.find('td:eq(4)').text(), | |
state: $row.find('td:eq(5)').text(), | |
zip: $row.find('td:eq(6)').text() | |
}); | |
} | |
}); | |
return JSON.stringify(rows); | |
}; | |
$(function(){ | |
console.log(convertTableToJson ()); | |
}); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment