As usual I create a class, but writes Uncaught ReferenceError: cell is not defined(…)

 var copyStyle = false; var cell = []; $('#sheet1__A2').mousedown(function() { $('.font-block .font-bold, .align-block .align-middle, .align-block .align-center').addClass('active-block') }); $('.copy-block .clear').click(function(){ $(this).addClass('active-block'); copyStyle = true; console.log('clear'); }); $('.default').mousedown(function(){ if (copyStyle) { console.log($(this)); cell.push($(this)); $('.default').bind('hover.selectCell', function() { cell.push($(this)); }); } }).mouseup(function(){ if (copyStyle) { $('.default').unbind('hover.selectCell'); for (var i = 0; i < cell.length; i++) { cell[i].css({ 'background-color' : '#ccc' }); } } }); 

  • which line is wrong? In the above code, such an error can not be. Try to provide a minimal reproducible example - Grundy
  • @Grundy doesn’t have any errors in the console, until I call the array. For example, cell[0] - Artem Holinka
  • that is, the code that is added to the question works then what's the mistake? where is cell [0] called? What should I do with the code provided so that the error is reproduced? - Grundy
  • @Grundy is all the js code that is used, for now. After the conditions under which copyStyle = true; And then in the console, I write cell[0] to check the values - Artem Holinka
  • Where is this code located and how do you run it? - Grundy

1 answer 1

The problem lies in the areas of visibility.

In the file, this code is located inside $(document).ready(function() {...}) .

When entering cell[0] into the console, this variable is searched in the global scope, while the desired variable is declared local to call $(document).ready .

There may be several solutions, for example

  1. do not type in the console, but output using console.log or similar functions
  2. make the cell variable global.
  3. put a stopping point inside the desired function, and at the moment of stopping the console's visibility area will correspond to the desired one.