# Selection

Class · extends `Events` · category: Other

Source: https://github.com/playcanvas/editor-api/blob/4303c34bcee9a418297350899ad3a3f555526ec4/src/selection.ts#L80

Selection API. Allows selecting Entities, Assets etc.

## Constructors

### constructor

```ts
new Selection()
```

Constructor

## Accessors

### count

```ts
get count(): number
```

Gets the number of selected items

### enabled

```ts
get enabled(): boolean
set enabled(value: boolean)
```

Gets enabled state of the selection methods.

### history

```ts
get history(): SelectionHistory
```

Gets the selection history

### item

```ts
get item(): Entity | Asset
```

Gets the first selected item. Short for this.items[0].

### items

```ts
get items(): (Entity | Asset)[]
```

Gets the selected items. This creates a new array every time it is called.

**Example**

```javascript
editor.selection.items.add(editor.entities.root);
const selectedEntities = editor.selection.items;
```

## Methods

### add

```ts
add(item: any, options?: object): void
```

Add item to selection

**Parameters**

- `item` (`any`)
- `options` (`object`, optional, default `{}`)
    - `options.history` (`boolean`, optional)

**Example**

```javascript
// add root entity to selection
editor.selection.add(editor.entities.root);
```

### clear

```ts
clear(options?: object): void
```

Clears selection

**Parameters**

- `options` (`object`, optional, default `{}`)
    - `options.history` (`boolean`, optional)

**Example**

```javascript
editor.selection.clear();
```

### has

```ts
has(item: any): boolean
```

Checks if item is in selection

**Parameters**

- `item` (`any`)

**Returns** `boolean`

**Example**

```javascript
const isRootSelected = editor.selection.has(editor.entities.root);
```

### remove

```ts
remove(item: any, options?: object): void
```

Remove item from selection

**Parameters**

- `item` (`any`)
- `options` (`object`, optional, default `{}`)
    - `options.history` (`boolean`, optional)

**Example**

```javascript
// remove root entity from selection
editor.selection.remove(editor.entities.root);
```

### set

```ts
set(items: any[], options?: object): void
```

Sets current selection

**Parameters**

- `items` (`any[]`)
- `options` (`object`, optional, default `{}`)
    - `options.history` (`boolean`, optional)

**Example**

```javascript
// select root entity
editor.selection.set([editor.entities.root]);
```

### toggle

```ts
toggle(item: any, options?: object): void
```

Toggle item selection

**Parameters**

- `item` (`any`)
- `options` (`object`, optional, default `{}`)
    - `options.history` (`boolean`, optional)

**Example**

```javascript
// toggle root entity selection
editor.selection.toggle(editor.entities.root);
```
