0

I have a array search_by_type which is declared at the top. Now in every click I am setting the rest_type_id value to array which is working fine.

Now other page action is calling get_all_restro() function which will empty the array search_by_type. When again I click restro_type_filter class it print array which has a value that means it is not getting empty.

var search_by_type = new Array();

$(document).on("click",".restro_type_filter",function(){

    console.log(search_by_type);

    var rest_type_id = $(this).attr('rest_type_id');
    search_by_type.push(rest_type_id); 


});

function get_all_restro(type){

    search_by_type.length = 0;

}

EDIT :

Ok guys there is an error at function get_all_restro() - Cannot set property 'length' of undefined how should I solve this? Is this error is creating a problem ?

What is happening here, what I am doing wrong ?

Thanks in advance.

16
  • stackoverflow.com/questions/206988/… Commented Jan 5, 2015 at 12:42
  • possible duplicate of Empty an array in JavaScript? Commented Jan 5, 2015 at 12:45
  • Can you build a minimal fiddle to reproduce this error ? Commented Jan 5, 2015 at 12:45
  • The problem is probably that the page action calling get_all_restro is itself not getting called. Do you know if the page action is ever called? Commented Jan 5, 2015 at 12:46
  • 1
    To people pretending you can't reset an array by setting the length to 0, here's an extract from the MDN : "You can set the length property to truncate an array at any time." Commented Jan 5, 2015 at 12:48

1 Answer 1

0

Try re-initializing it this:

search_by_type = new Array();

or

search_by_type = [];

Complete code:

function get_all_restro(type){

    search_by_type = new Array();

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.