I am building an error list. When click the submission button, the old errors will be removed and the new errors will be added. The following code is what I am using but it keeps adding new errors without removing the old ones: (my JS fiddle here: http://jsfiddle.net/shapeare/bc1bdq1b/)
<div id="error">
Below is a list of errors:
</div>
<input id="submitBtn" type="submit" value="submit"/>
$(document).ready(function(){
$index = 0;
$(document).on('click', '#submitBtn', function(event) {
$('#error').innerHTML = '';
$('#error').append('<p>Error ' + $index + '</p>');
$index ++;
});
});
What I want to achieve is that every time the button is clicked, the old error disappears and a new error occurs. For example, the first time the submit button is clicked, "Error 0" will appear. The second time it is clicked, "Error 0" disappears and "Error 1" is shown.