This is the script:
$("#some_button").click(function() {
var changed = [];
$( 'input[id$="_1"]' ).each(function() {
var new_id = this.id.replace( '_1', '_0' );
if ( $(this).val() !== $( 'input#' + new_id ).val() ) {
changed.push({id:new_id, new_val:$('input#' + new_id).val(), old_val:$(this).val()});
}
});
alert(changed);
});
and it’s gives me [object Object],[object Object].
What am I doing wrong?
Because you don’t have JSON. You have an array:
[]. JSON is a string representation of a javascript object.You could use the
JSON.stringifymethod to generate a JSON string from an existing object:The
JSON.stringifymethod is native in modern browsers but if you need to support legacy browsers you will need to include json2.js to your page. This script checks whether the browser supports nativelyJSON.stringifyand uses it, or if it doesn’t it provides a sample implementation.