Skip to content

Select a single cell, a range of adjacent cells, or multiple non-adjacent ranges of cells.

Overview

Selection enables you to select a single cell or ranges of cells within Handsontable. Once selected, you can retrieve data from the cell, edit the cell’s contents, or change the style of the cell.

Basic configuration

With this feature, you can select single cells or ranges of cells across a grid. Easily retrieve the coordinates of the selected cells to clear or change the cells’ content.

Use Cmd on Mac or Ctrl on Windows to select non-adjacent ranges of cells.

Select ranges

There are different modes in which you can use this plugin. Choose between selecting a single cell, a range of adjacent cells, and multiple non-adjacent ranges of cells.

Possible values of selectionMode:

  • single - You can select a single cell.
  • range - You can select multiple cells within a single rangeselected.
  • multiple - You can select multiple, non-adjacent ranges of cells.
JavaScript
import Handsontable from 'handsontable/base';
import { registerAllModules } from 'handsontable/registry';
// Register all Handsontable's modules.
registerAllModules();
const container = document.querySelector('#example1');
const hot = new Handsontable(container, {
data: [
['A1', 'B1', 'C1', 'D1', 'E1', 'F1', 'G1', 'H1', 'I1'],
['A2', 'B2', 'C2', 'D2', 'E2', 'F2', 'G2', 'H2', 'I2'],
['A3', 'B3', 'C3', 'D3', 'E3', 'F3', 'G3', 'H3', 'I3'],
['A4', 'B4', 'C4', 'D4', 'E4', 'F4', 'G4', 'H4', 'I4'],
['A5', 'B5', 'C5', 'D5', 'E5', 'F5', 'G5', 'H5', 'I5'],
['A6', 'B6', 'C6', 'D6', 'E6', 'F6', 'G6', 'H6', 'I6'],
['A7', 'B7', 'C7', 'D7', 'E7', 'F7', 'G7', 'H7', 'I7'],
['A8', 'B8', 'C8', 'D8', 'E8', 'F8', 'G8', 'H8', 'I8'],
['A9', 'B9', 'C9', 'D9', 'E9', 'F9', 'G9', 'H9', 'I9'],
],
width: 'auto',
height: 'auto',
colWidths: 100,
rowHeaders: true,
colHeaders: true,
selectionMode: 'multiple',
autoWrapRow: true,
autoWrapCol: true,
licenseKey: 'non-commercial-and-evaluation',
});
const dropdown = document.querySelector('#selectionDropdown');
const trigger = document.querySelector('#selectionTrigger');
const menu = document.querySelector('#selectionMenu');
const label = document.querySelector('#selectionLabel');
trigger.addEventListener('click', () => {
const isOpen = !menu.hidden;
menu.hidden = isOpen;
trigger.setAttribute('aria-expanded', String(!isOpen));
});
menu.addEventListener('click', (e) => {
const item = e.target.closest('li[data-value]');
if (item) {
label.textContent = item.textContent.trim();
menu.querySelectorAll('li').forEach((li) => li.setAttribute('aria-selected', 'false'));
item.setAttribute('aria-selected', 'true');
menu.hidden = true;
trigger.setAttribute('aria-expanded', 'false');
hot.updateSettings({ selectionMode: item.dataset.value });
}
});
document.addEventListener('click', (e) => {
if (!dropdown.contains(e.target)) {
menu.hidden = true;
trigger.setAttribute('aria-expanded', 'false');
}
});
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape' && !menu.hidden) {
menu.hidden = true;
trigger.setAttribute('aria-expanded', 'false');
trigger.focus();
}
});
TypeScript
import Handsontable from 'handsontable/base';
import { registerAllModules } from 'handsontable/registry';
// Register all Handsontable's modules.
registerAllModules();
const container = document.querySelector('#example1')!;
const hot = new Handsontable(container, {
data: [
['A1', 'B1', 'C1', 'D1', 'E1', 'F1', 'G1', 'H1', 'I1'],
['A2', 'B2', 'C2', 'D2', 'E2', 'F2', 'G2', 'H2', 'I2'],
['A3', 'B3', 'C3', 'D3', 'E3', 'F3', 'G3', 'H3', 'I3'],
['A4', 'B4', 'C4', 'D4', 'E4', 'F4', 'G4', 'H4', 'I4'],
['A5', 'B5', 'C5', 'D5', 'E5', 'F5', 'G5', 'H5', 'I5'],
['A6', 'B6', 'C6', 'D6', 'E6', 'F6', 'G6', 'H6', 'I6'],
['A7', 'B7', 'C7', 'D7', 'E7', 'F7', 'G7', 'H7', 'I7'],
['A8', 'B8', 'C8', 'D8', 'E8', 'F8', 'G8', 'H8', 'I8'],
['A9', 'B9', 'C9', 'D9', 'E9', 'F9', 'G9', 'H9', 'I9'],
],
width: 'auto',
height: 'auto',
colWidths: 100,
rowHeaders: true,
colHeaders: true,
selectionMode: 'multiple', // 'single', 'range' or 'multiple',
autoWrapRow: true,
autoWrapCol: true,
licenseKey: 'non-commercial-and-evaluation',
});
const dropdown = document.querySelector('#selectionDropdown')!;
const trigger = document.querySelector('#selectionTrigger')! as HTMLButtonElement;
const menu = document.querySelector('#selectionMenu')! as HTMLUListElement;
const label = document.querySelector('#selectionLabel')!;
trigger.addEventListener('click', () => {
const isOpen = !menu.hidden;
menu.hidden = isOpen;
trigger.setAttribute('aria-expanded', String(!isOpen));
});
menu.addEventListener('click', (e: MouseEvent) => {
const item = (e.target as HTMLElement).closest('li[data-value]') as HTMLLIElement | null;
if (item) {
label.textContent = item.textContent!.trim();
menu.querySelectorAll('li').forEach((li) => li.setAttribute('aria-selected', 'false'));
item.setAttribute('aria-selected', 'true');
menu.hidden = true;
trigger.setAttribute('aria-expanded', 'false');
hot.updateSettings({ selectionMode: item.dataset.value } as Handsontable.GridSettings);
}
});
document.addEventListener('click', (e: MouseEvent) => {
if (!dropdown.contains(e.target as Node)) {
menu.hidden = true;
trigger.setAttribute('aria-expanded', 'false');
}
});
document.addEventListener('keydown', (e: KeyboardEvent) => {
if (e.key === 'Escape' && !menu.hidden) {
menu.hidden = true;
trigger.setAttribute('aria-expanded', 'false');
trigger.focus();
}
});
HTML
<div class="example-controls-container">
<div class="controls">
<div class="theme-dropdown" id="selectionDropdown">
<button class="theme-dropdown-trigger" id="selectionTrigger" type="button" aria-haspopup="listbox" aria-expanded="false">
<span id="selectionLabel">Multiple ranges selection</span>
<svg class="theme-dropdown-chevron" aria-hidden="true" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M6 9l6 6l6 -6"/></svg>
</button>
<ul class="theme-dropdown-menu" id="selectionMenu" role="listbox" hidden>
<li role="option" data-value="single">Single selection</li>
<li role="option" data-value="range">Range selection</li>
<li role="option" data-value="multiple" aria-selected="true">Multiple ranges selection</li>
</ul>
</div>
</div>
</div>
<div id="example1"></div>
CSS
#example1 {
z-index: 1;
position: relative;
}

Get data from the selected ranges

To retrieve the selected cells as an array of arrays, you use the getSelected() or getSelectedRange() methods.

Here you will see the log
JavaScript
import Handsontable from 'handsontable/base';
import { registerAllModules } from 'handsontable/registry';
// Register all Handsontable's modules.
registerAllModules();
const container = document.querySelector('#example2');
const hot = new Handsontable(container, {
data: [
['A1', 'B1', 'C1', 'D1', 'E1', 'F1', 'G1', 'H1', 'I1'],
['A2', 'B2', 'C2', 'D2', 'E2', 'F2', 'G2', 'H2', 'I2'],
['A3', 'B3', 'C3', 'D3', 'E3', 'F3', 'G3', 'H3', 'I3'],
['A4', 'B4', 'C4', 'D4', 'E4', 'F4', 'G4', 'H4', 'I4'],
['A5', 'B5', 'C5', 'D5', 'E5', 'F5', 'G5', 'H5', 'I5'],
['A6', 'B6', 'C6', 'D6', 'E6', 'F6', 'G6', 'H6', 'I6'],
['A7', 'B7', 'C7', 'D7', 'E7', 'F7', 'G7', 'H7', 'I7'],
['A8', 'B8', 'C8', 'D8', 'E8', 'F8', 'G8', 'H8', 'I8'],
['A9', 'B9', 'C9', 'D9', 'E9', 'F9', 'G9', 'H9', 'I9'],
],
width: 'auto',
height: 'auto',
colWidths: 100,
rowHeaders: true,
colHeaders: true,
outsideClickDeselects: false,
selectionMode: 'multiple',
autoWrapRow: true,
autoWrapCol: true,
licenseKey: 'non-commercial-and-evaluation',
});
const getButton = document.querySelector('#getButton');
const output = document.querySelector('#output');
getButton.addEventListener('click', () => {
const selected = hot.getSelected() || [];
let data = [];
if (selected.length === 1) {
data = hot.getData(...selected[0]);
} else {
for (let i = 0; i < selected.length; i += 1) {
data.push(hot.getData(...selected[i]));
}
}
output.innerText = JSON.stringify(data);
});
TypeScript
import Handsontable from 'handsontable/base';
import { registerAllModules } from 'handsontable/registry';
// Register all Handsontable's modules.
registerAllModules();
const container = document.querySelector('#example2')!;
const hot = new Handsontable(container, {
data: [
['A1', 'B1', 'C1', 'D1', 'E1', 'F1', 'G1', 'H1', 'I1'],
['A2', 'B2', 'C2', 'D2', 'E2', 'F2', 'G2', 'H2', 'I2'],
['A3', 'B3', 'C3', 'D3', 'E3', 'F3', 'G3', 'H3', 'I3'],
['A4', 'B4', 'C4', 'D4', 'E4', 'F4', 'G4', 'H4', 'I4'],
['A5', 'B5', 'C5', 'D5', 'E5', 'F5', 'G5', 'H5', 'I5'],
['A6', 'B6', 'C6', 'D6', 'E6', 'F6', 'G6', 'H6', 'I6'],
['A7', 'B7', 'C7', 'D7', 'E7', 'F7', 'G7', 'H7', 'I7'],
['A8', 'B8', 'C8', 'D8', 'E8', 'F8', 'G8', 'H8', 'I8'],
['A9', 'B9', 'C9', 'D9', 'E9', 'F9', 'G9', 'H9', 'I9'],
],
width: 'auto',
height: 'auto',
colWidths: 100,
rowHeaders: true,
colHeaders: true,
outsideClickDeselects: false,
selectionMode: 'multiple', // 'single', 'range' or 'multiple',
autoWrapRow: true,
autoWrapCol: true,
licenseKey: 'non-commercial-and-evaluation',
});
const getButton = document.querySelector('#getButton')!;
const output = document.querySelector('#output')!;
getButton.addEventListener('click', () => {
const selected = hot.getSelected() || [];
let data: Handsontable.CellValue[] = [];
if (selected.length === 1) {
data = hot.getData(...selected[0]!);
} else {
for (let i = 0; i < selected.length; i += 1) {
data.push(hot.getData(...selected[i]!));
}
}
(output as HTMLElement).innerText = JSON.stringify(data);
});
HTML
<div class="example-controls-container">
<div class="controls">
<button id="getButton">Get data</button>
</div>
<output class="console" id="output">Here you will see the log</output>
</div>
<div id="example2"></div>

Modify the selected cells

You may want to delete, format, or otherwise change the selected cells. For example, you can change a value or add CSS classes to the selected cells using the demo below.

JavaScript
import Handsontable from 'handsontable/base';
import { registerAllModules } from 'handsontable/registry';
// Register all Handsontable's modules.
registerAllModules();
const container = document.querySelector('#example3');
const hot = new Handsontable(container, {
data: [
['A1', 'B1', 'C1', 'D1', 'E1', 'F1', 'G1', 'H1', 'I1'],
['A2', 'B2', 'C2', 'D2', 'E2', 'F2', 'G2', 'H2', 'I2'],
['A3', 'B3', 'C3', 'D3', 'E3', 'F3', 'G3', 'H3', 'I3'],
['A4', 'B4', 'C4', 'D4', 'E4', 'F4', 'G4', 'H4', 'I4'],
['A5', 'B5', 'C5', 'D5', 'E5', 'F5', 'G5', 'H5', 'I5'],
['A6', 'B6', 'C6', 'D6', 'E6', 'F6', 'G6', 'H6', 'I6'],
['A7', 'B7', 'C7', 'D7', 'E7', 'F7', 'G7', 'H7', 'I7'],
['A8', 'B8', 'C8', 'D8', 'E8', 'F8', 'G8', 'H8', 'I8'],
['A9', 'B9', 'C9', 'D9', 'E9', 'F9', 'G9', 'H9', 'I9'],
],
width: 'auto',
height: 'auto',
colWidths: 100,
rowHeaders: true,
colHeaders: true,
outsideClickDeselects: false,
selectionMode: 'multiple',
autoWrapRow: true,
autoWrapCol: true,
licenseKey: 'non-commercial-and-evaluation',
});
const button = document.querySelector('#set-data-action');
button.addEventListener('click', () => {
const selected = hot.getSelected() || [];
hot.suspendRender();
for (let index = 0; index < selected.length; index += 1) {
const [row1, column1, row2, column2] = selected[index];
const startRow = Math.max(Math.min(row1, row2), 0);
const endRow = Math.max(row1, row2);
const startCol = Math.max(Math.min(column1, column2), 0);
const endCol = Math.max(column1, column2);
for (let rowIndex = startRow; rowIndex <= endRow; rowIndex += 1) {
for (let columnIndex = startCol; columnIndex <= endCol; columnIndex += 1) {
hot.setDataAtCell(rowIndex, columnIndex, 'data changed');
hot.setCellMeta(rowIndex, columnIndex, 'className', 'c-red');
}
}
}
hot.render();
hot.resumeRender();
});
TypeScript
import Handsontable from 'handsontable/base';
import { registerAllModules } from 'handsontable/registry';
// Register all Handsontable's modules.
registerAllModules();
const container = document.querySelector('#example3')!;
const hot = new Handsontable(container, {
data: [
['A1', 'B1', 'C1', 'D1', 'E1', 'F1', 'G1', 'H1', 'I1'],
['A2', 'B2', 'C2', 'D2', 'E2', 'F2', 'G2', 'H2', 'I2'],
['A3', 'B3', 'C3', 'D3', 'E3', 'F3', 'G3', 'H3', 'I3'],
['A4', 'B4', 'C4', 'D4', 'E4', 'F4', 'G4', 'H4', 'I4'],
['A5', 'B5', 'C5', 'D5', 'E5', 'F5', 'G5', 'H5', 'I5'],
['A6', 'B6', 'C6', 'D6', 'E6', 'F6', 'G6', 'H6', 'I6'],
['A7', 'B7', 'C7', 'D7', 'E7', 'F7', 'G7', 'H7', 'I7'],
['A8', 'B8', 'C8', 'D8', 'E8', 'F8', 'G8', 'H8', 'I8'],
['A9', 'B9', 'C9', 'D9', 'E9', 'F9', 'G9', 'H9', 'I9'],
],
width: 'auto',
height: 'auto',
colWidths: 100,
rowHeaders: true,
colHeaders: true,
outsideClickDeselects: false,
selectionMode: 'multiple', // 'single', 'range' or 'multiple',
autoWrapRow: true,
autoWrapCol: true,
licenseKey: 'non-commercial-and-evaluation',
});
const button = document.querySelector('#set-data-action')!;
button.addEventListener('click', () => {
const selected = hot.getSelected() || [];
hot.suspendRender();
for (let index = 0; index < selected.length; index += 1) {
const [row1, column1, row2, column2] = selected[index]!;
const startRow = Math.max(Math.min(row1, row2), 0);
const endRow = Math.max(row1, row2);
const startCol = Math.max(Math.min(column1, column2), 0);
const endCol = Math.max(column1, column2);
for (let rowIndex = startRow; rowIndex <= endRow; rowIndex += 1) {
for (let columnIndex = startCol; columnIndex <= endCol; columnIndex += 1) {
hot.setDataAtCell(rowIndex, columnIndex, 'data changed');
hot.setCellMeta(rowIndex, columnIndex, 'className', 'c-red');
}
}
}
hot.render();
hot.resumeRender();
});
HTML
<div class="example-controls-container">
<div class="controls">
<button id="set-data-action">Click to modify the selected cells</button>
</div>
</div>
<div id="example3"></div>
CSS
.c-red {
color: red;
}

Style the selection area

You can easily change the background color, using CSS styles. The main, light blue background color is defined in the .area class.

For non-adjacent selection, multiple classes are making each level a bit darker. These classes are called area-1, area-2, etc.

Unfortunately, there is no easy way to change the border color of the selection.

Jump across the grid’s edges

When you use keyboard navigation to cross an edge of the grid, you can set cell selection to jump to the opposite edge.

Jump across vertical edges

To enable jumping across the left and right edges:

To jump across a vertical edge:

  • When cell selection is on a row’s first cell, press the left arrow key.
  • When cell selection is on a row’s last cell, press the right arrow key, or press Tab.

Jump across horizontal edges

To enable jumping across the top and bottom edges:

To jump across a horizontal edge:

  • When cell selection is on a column’s first cell, press the up arrow key.
  • When cell selection is on a column’s last cell, press the down arrow key, or press Enter.
WindowsmacOSActionExcelSheets
Ctrl+ACmd+ASelect all cells
Ctrl+Shift+SpaceCmd+Shift+SpaceSelect all cells and headers
Ctrl+SpaceCtrl+SpaceSelect the entire column
Shift+SpaceShift+SpaceSelect the entire row
Ctrl+Shift+Cmd+Shift+Extend the selection to the first cell of the current column**
Ctrl+Shift+Cmd+Shift+Extend the selection to the last cell of the current column**
Ctrl+Shift+Cmd+Shift+Extend the selection to the leftmost cell of the current row**
Ctrl+Shift+Cmd+Shift+Extend the selection to the rightmost cell of the current row**
Shift + Arrow keysShift + Arrow keysExtend the selection by one cell
Shift+HomeShift+HomeExtend the selection to the first non-frozen cell of the current row*
Shift+EndShift+EndExtend the selection to the last non-frozen cell of the current row*
Shift+Page UpShift+Page UpExtend the selection by one screen up
Shift+Page DownShift+Page DownExtend the selection by one screen down
Ctrl+EnterCmd+EnterFill the selected range of cells with the value of the active cell
DeleteDeleteClear the contents of the selected cells
BackspaceBackspaceClear the contents of the selected cells

* This action depends on your layout direction.
** In case of multiple selection layers, only the last selection layer gets extended.

Configuration options

Core methods

Hooks

Plugins