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.

Rakesh Shetty
  • 4,548
  • 7
  • 40
  • 79
  • http://stackoverflow.com/questions/206988/how-do-i-unset-an-element-in-an-array-in-javascript – Alexis Peters Jan 05 '15 at 12:42
  • possible duplicate of [Empty an array in JavaScript?](http://stackoverflow.com/questions/1232040/empty-an-array-in-javascript) – jbutler483 Jan 05 '15 at 12:45
  • Can you build a minimal fiddle to reproduce this error ? – Denys Séguret Jan 05 '15 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? – axelduch Jan 05 '15 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."* – Denys Séguret Jan 05 '15 at 12:48
  • @dystroy please see my edit. There is an error on console – Rakesh Shetty Jan 05 '15 at 12:53
  • `This question may already have an answer here:` dude I have already seen that and I have told you guys this is not working in my case. Please allow people to add answer for my problem – Rakesh Shetty Jan 05 '15 at 12:54
  • @jbutler483 I have already saw that question and tried too as well in my case it is not working – Rakesh Shetty Jan 05 '15 at 12:56
  • @RakeshShetty Make it possible for us to reproduce your problem. Use http://jsbin.com for example – Denys Séguret Jan 05 '15 at 12:56
  • Then could you provide a minimal, reproducible example, and also explain your efforts so that we're not answering something you've already tried? – jbutler483 Jan 05 '15 at 12:57
  • @dystroy I will try to add code on fiddle it will be not excat code since it is very long code but will try to paste some of the code there – Rakesh Shetty Jan 05 '15 at 12:59
  • @dystroy can you look into this http://jsfiddle.net/s1jrp9x0/1/ but it is giving error `get_all_restro is not defined ` – Rakesh Shetty Jan 05 '15 at 13:14
  • @RakeshShetty In the left menu choose "no wrap " – Denys Séguret Jan 05 '15 at 13:18
  • @RakeshShetty Looks like a problem of scope. If you define the function within `$(document).ready(function(){//here//});` it's not going to be visible outside and therefore `onclick="get_all_restro(1);"` is going to fail. – acontell Jan 05 '15 at 13:19
  • @dystroy it is running on fiddle but in my code is not running. What is "no wrap " what should I change in my code ? – Rakesh Shetty Jan 05 '15 at 13:20
  • @dystroy I have not wrap inside `$(document).ready(function()` – Rakesh Shetty Jan 05 '15 at 13:26

1 Answers1

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();

}
Manwal
  • 23,450
  • 12
  • 63
  • 93