Search code examples
javascriptprototypejs

How to pass a parameter to an inline javascript function


I'm trying to pass story_id into the innermost inline function, but its always 0 even though its not 0 in the outer function. How do I pass this value in?

function activateSearch(){
  if($('story_filter')) Event.observe('story_filter', 'keyup',
    function(event) {
      $('stories_table').select(".story").each(
        function(story) {
          story_id = story.id.split('_')[1];
          story.select('.tableCell', '.indexCardContent').each(
            function(task, story_id) {
              hideStoryRow(story_id);
              task_filter = new RegExp($F('story_filter'), "i");
              if (task.innerHTML.match( task_filter ))
              {
                  showStoryRow(story_id);
                  throw $break;
              }
            }
          );
        }
      );
    }
  );
}

Solution

  • All you have to do to make this work is change this line:

        function(task, story_id) {
    

    to this:

        function(task) {
    

    You don't have to pass story_id anywhere -- it's already in scope in the inner function!