I am trying to remove all the elements of the array using array = [];
when the div value becomes 'clear all' but the array is not able to clear.
All i am doing here is including the selected checkbox value in the div called 'filtervalue'.
here is the script
<script type="text/javascript">
$('.filtercheck, #location, #sorting , #filtervalue').on('click', function(e) {
var parameter = [];
var filter = [];
var index = "";
var HTML = "";
var newHTML = "";
$(".filtercheck").each(function() {
if (this.checked) {
filter.push($(this).val())
parameter.push(this.name + "=" + $(this).val());
}
});
if ($("#location").val()) {
parameter.push($("#location").val());
}
if ($('#sorting').val()) {
parameter.push($('#sorting').val());
}
for (var i = 0; i < filter.length; i++) {
HTML = HTML + '<div>' + filter[i] + '</div>';
}
newHTML = HTML + '<div>' + 'Clear All' + '</div>'
$('#filtervalue').html(newHTML).show();
if (filter.length == 0) {
$('#filtervalue').hide();
}
$('div#filtervalue>div').each(function() {
$(this).on("click", function() {
text = ($(this).text());
if (text == 'Clear All') {
filter = [];
console.log(filter.length)
parameter = [];
console.log(parameter.length)
$('#filtervalue').hide();
} else {
filter = jQuery.grep(filter, function(n) {
return n != text;
});
console.log(filter)
console.log(parameter)
checkbox = $(":checkbox[value=" + text + "]")
if (checkbox.prop("checked", true)) {
checkbox.prop("checked", false);
$(this).remove();
}
}
});
});
parameter = parameter.join('&');
$('#loading-image').show();
console.log(filter)
console.log(parameter)
$.ajax({
url: '{{ instance.get_absolute_url }}',
type: 'GET',
data: parameter,
success: function(data) {
var a = $(data).find('.prodthumb');
$('.productgrid').html(a);
$("img.lazy").lazyload();
},
error: function(e) {
console.log(e.message);
},
complete: function() {
$('#loading-image').hide();
}
});
});
</script>
How can i remove all the elements of the array? Thank you