...

/

The Enter Selection

The Enter Selection

D3 introduces two new selections called enter and exit. Let's discuss what the enter selection is

The data() method we used previously returned a selection object. However, the selection object is slightly different from other selection objects we have worked with thus far. There are two new properties called _enter and _exit. We will be discussing what the enter selection is in this lesson.

What is the enter selection?

Joining data can become complex when the ratio between data and elements is not 1-to-1. If we have more elements than we have data, we should remove excess elements. If there are fewer elements than we have data, we should add more elements.

In the developer tools, the selection object returned was different from the previous selection objects.

A new property is available called _enter. This is called the enter selection. The enter selection contains a list of data items that have not been joined to an element. Let’s take a look inside this array.

We will find an empty array. That is because every piece of data was joined to an element. That may not always be the case. Let’s look at what happens when there are not enough elements in the document.

In the widget above, the three <li> elements have been removed. The JavaScript code remains the same. If we look at the output, nothing changes on the document. However, the selection logged in the console is different.

This time, the enter selection is not empty anymore. The enter selection will have a total of 5 items in the ...

Access this course and 1400+ top-rated courses and projects.