.push: creates new object instead of overwriting the same
Hi,
I searched a lot and tried a lot of things but I can't get it to work. Currently, if I press the button th first time it pushes the data to the array which is fine. But if I press the second or third time the button it creates a new object in the array. How can I avoid that? It should just overwrite it.
Hope someone can give me a hint. Thanks in advance.
Here's a simplified code just with the problem:
- var Validator = {
data_checked_result: [],
errors: [],
addRule: function (finder, rule, error, live) {
var isValid = true; //here are other functions called but for the test is not needed to keep it simple;
this.data_checked_result.push({is_valid: isValid, name: finder['name'], error_message: error});
alert(this.data_checked_result);
}
} //end var
$(function(){
$('#btn-save').button().click(function() {
Validator.addRule({type: 'input', name: 'category_title_1'}, { type: 'string', minLength: 5 }, 'ERROR', false);
Validator.addRule({type: 'input', name: 'category_title_2'}, { type: 'string', minLength: 5 }, 'ERROR', false);
});
});