---
title: "Javascript Event calendar Event Hooks Example | Mobiscroll"
component: "Event calendar"
framework: "JavaScript"
library_version: "6.1.5"
url: "https://demo.mobiscroll.com/javascript/eventcalendar/event-hooks"
---

# Event Hooks - JavaScript Event calendar demo

See which lifecycle hooks the Javascript event calendar fires on create, drag, hover, and click — interactions are tracked in a live event log. Plain JavaScript API.

- [Live demo](https://demo.mobiscroll.com/javascript/eventcalendar/event-hooks)
- [View code on GitHub](https://github.com/acidb/mobiscroll-demos-javascript/tree/main/src/demos/eventcalendar/calendar-view/event-hooks)
- This demo in other frameworks: [jQuery](https://demo.mobiscroll.com/jquery/eventcalendar/event-hooks.md), [Angular](https://demo.mobiscroll.com/angular/eventcalendar/event-hooks.md), [React](https://demo.mobiscroll.com/react/eventcalendar/event-hooks.md), [Vue](https://demo.mobiscroll.com/vue/eventcalendar/event-hooks.md)

## Description

The event calendar ships with different hooks for deep customization. Events are triggered through the lifecycle of the component where you can tie in custom functionality and code.

While users interact with the UI events like `onEventClick`, `onInit`, `onSelectedDateChange` ... will be triggered.

**For a complete list of events go to the documentation** [See available lifecycle events →](https://mobiscroll.com/docs/javascript/eventcalendar/api#events)

## What this demo shows

- A desktop month-view event calendar that demonstrates lifecycle events triggered by calendar interactions.
- **External drag & drop** Two external events are shown above the calendar and can be dragged onto the month grid.
- **Month grid** The calendar displays a full month view with weekends disabled.
- **Event labels** Days with events show colored labels inside the day cells, with different colors distinguishing different events.
- **Overflow handling** The number of visible event labels depends on the available height in each day cell. Additional events are collapsed behind an `X more` link.
- **Popover** Clicking the `X more` link opens a popover that shows the hidden events for that day.
- **Label interaction** Hovering over or clicking an event label selects it and highlights the selected label.
- **Day cell states for future days** Hovering a day cell highlights the day number with a gray background, while clicking the empty part of the cell selects the day and highlights the day number with a blue background.
- **Event creation** Users can create events by clicking and dragging across calendar cells or by double-clicking a day cell.
- **Calendar header** The header shows the current month and year on the left, and blue month navigation arrows with a `Today` button (for jumping back to the current date) between them on the right. 
- **Event log** An Event log panel is shown at the left and lists the lifecycle events fired during interactions such as event creation, hover, drag, and similar actions.

## Best for

- **Lifecycle event discovery** Understanding which Event calendar lifecycle hooks fire during common user interactions.

## Tutorial

```js
onCellClick: function (event, inst) { /* Logic for handling cell clicks */ },
onCellDoubleClick: function (event, inst) { /* Logic for handling cell double clicks */ },
onCellRightClick: function (event, inst) { /* Logic for handling cell right clicks */ },
onCellHoverIn: function (event, inst) { /* Logic for handling cell hover in */ },
onCellHoverOut: function (event, inst) { /* Logic for handling cell hover out */ },
onDestroy: function (event, inst) { /* Logic for destroying the event calendar */ },
onEventClick: function (event, inst) { /* Logic for event click */ },
onEventCreate: function (event, inst) { /* Logic for event create */ },
onEventCreated: function (event, inst) { /* Logic for event created */ },
onEventCreateFailed: function (event, inst) { /* Logic for failed event create */ },
onEventDelete: function (event, inst) { /* Logic for event delete */ },
onEventDeleted: function (event, inst) { /* Logic for event deleted */ },
onEventDoubleClick: function (event, inst) { /* Logic for event double click */ },
onEventDragEnd: function (event, inst) { /* Triggered when event drag ends */ },
onEventDragStart: function (event, inst) { /* Triggered when event drag is started */ },
onEventDragEnter: function (event, inst) { /* Triggered when a dragged event enters the calendar */ }
onEventDragLeave: function (event, inst) { /* Triggered when a dragged event leaves the calendar */ }
onEventHoverIn: function (event, inst) { /* Logic for event hover in */ },
onEventHoverOut: function (event, inst) { /* Logic for event hover out */ },
onEventRightClick: function (event, inst) { /* Logic for event right click */ },
onEventUpdate: function (event, inst) { /* Logic for event update */ },
onEventUpdated: function (event, inst) { /* This is where you save the updated event to your DB */ },
onEventUpdateFailed: function (event, inst) { /* Logic for failed event update */ },
onInit: function (event, inst) { /* Logic running on component init */ },
onLabelClick: function (event, inst) { /* Logic for label click */ },
onPageChange: function (event, inst) { /* Your custom event handler goes here */ },
onPageLoaded: function (event, inst) { /* Use it to inject custom markup & attach custom listeners */ },
onPageLoading: function (event, inst) { /* Use it to load data on demand */ },
onSelectedDateChange: function (event, inst) { /* Use it to keep track of the selected date externally */ }
```

## API used in this demo

- [`theme`](https://mobiscroll.com/docs/javascript/eventcalendar/api#opt-theme) - option
- [`themeVariant`](https://mobiscroll.com/docs/javascript/eventcalendar/api#opt-themeVariant) - option
- [`dragToCreate`](https://mobiscroll.com/docs/javascript/eventcalendar/api#opt-dragToCreate) - option
- [`dragToMove`](https://mobiscroll.com/docs/javascript/eventcalendar/api#opt-dragToMove) - option
- [`dragToResize`](https://mobiscroll.com/docs/javascript/eventcalendar/api#opt-dragToResize) - option
- [`externalDrop`](https://mobiscroll.com/docs/javascript/eventcalendar/api#opt-externalDrop) - option
- [`invalid`](https://mobiscroll.com/docs/javascript/eventcalendar/api#opt-invalid) - option
- [`view`](https://mobiscroll.com/docs/javascript/eventcalendar/api#opt-view) - option
- [`labels`](https://mobiscroll.com/docs/javascript/eventcalendar/api#opt-labels) - option
- [`onCellClick`](https://mobiscroll.com/docs/javascript/eventcalendar/api#event-onCellClick) - event
- [`onCellDoubleClick`](https://mobiscroll.com/docs/javascript/eventcalendar/api#event-onCellDoubleClick) - event
- [`onCellRightClick`](https://mobiscroll.com/docs/javascript/eventcalendar/api#event-onCellRightClick) - event
- [`onCellHoverIn`](https://mobiscroll.com/docs/javascript/eventcalendar/api#event-onCellHoverIn) - event
- [`onCellHoverOut`](https://mobiscroll.com/docs/javascript/eventcalendar/api#event-onCellHoverOut) - event
- [`onDestroy`](https://mobiscroll.com/docs/javascript/eventcalendar/api#event-onDestroy) - event
- [`onEventClick`](https://mobiscroll.com/docs/javascript/eventcalendar/api#event-onEventClick) - event
- [`onEventCreate`](https://mobiscroll.com/docs/javascript/eventcalendar/api#event-onEventCreate) - event
- [`onEventCreated`](https://mobiscroll.com/docs/javascript/eventcalendar/api#event-onEventCreated) - event
- [`onEventCreateFailed`](https://mobiscroll.com/docs/javascript/eventcalendar/api#event-onEventCreateFailed) - event
- [`onEventDelete`](https://mobiscroll.com/docs/javascript/eventcalendar/api#event-onEventDelete) - event
- [`onEventDeleted`](https://mobiscroll.com/docs/javascript/eventcalendar/api#event-onEventDeleted) - event
- [`onEventDoubleClick`](https://mobiscroll.com/docs/javascript/eventcalendar/api#event-onEventDoubleClick) - event
- [`onEventDragStart`](https://mobiscroll.com/docs/javascript/eventcalendar/api#event-onEventDragStart) - event
- [`onEventDragEnd`](https://mobiscroll.com/docs/javascript/eventcalendar/api#event-onEventDragEnd) - event
- [`onEventDragEnter`](https://mobiscroll.com/docs/javascript/eventcalendar/api#event-onEventDragEnter) - event
- [`onEventDragLeave`](https://mobiscroll.com/docs/javascript/eventcalendar/api#event-onEventDragLeave) - event
- [`onEventHoverIn`](https://mobiscroll.com/docs/javascript/eventcalendar/api#event-onEventHoverIn) - event
- [`onEventHoverOut`](https://mobiscroll.com/docs/javascript/eventcalendar/api#event-onEventHoverOut) - event
- [`onEventUpdate`](https://mobiscroll.com/docs/javascript/eventcalendar/api#event-onEventUpdate) - event
- [`onEventUpdated`](https://mobiscroll.com/docs/javascript/eventcalendar/api#event-onEventUpdated) - event
- [`onEventUpdateFailed`](https://mobiscroll.com/docs/javascript/eventcalendar/api#event-onEventUpdateFailed) - event
- [`onEventRightClick`](https://mobiscroll.com/docs/javascript/eventcalendar/api#event-onEventRightClick) - event
- [`onInit`](https://mobiscroll.com/docs/javascript/eventcalendar/api#event-onInit) - event
- [`onLabelClick`](https://mobiscroll.com/docs/javascript/eventcalendar/api#event-onLabelClick) - event
- [`onPageChange`](https://mobiscroll.com/docs/javascript/eventcalendar/api#event-onPageChange) - event
- [`onPageLoaded`](https://mobiscroll.com/docs/javascript/eventcalendar/api#event-onPageLoaded) - event
- [`onPageLoading`](https://mobiscroll.com/docs/javascript/eventcalendar/api#event-onPageLoading) - event
- [`onSelectedDateChange`](https://mobiscroll.com/docs/javascript/eventcalendar/api#event-onSelectedDateChange) - event
- [`setEvents`](https://mobiscroll.com/docs/javascript/eventcalendar/api#method-setEvents) - method

## Code

### JS

```js
mobiscroll.setOptions({
  theme: 'ios',
  themeVariant: 'light'
});

var inst = mobiscroll.eventcalendar('#demo', {
  dragToCreate: true,
  dragToMove: true,
  dragToResize: true,
  externalDrop: true,
  invalid: [
    {
      recurring: {
        repeat: 'weekly',
        weekDays: 'SA,SU',
      },
    },
  ],
  view: {
    calendar: { labels: true },
  },
  onCellClick: function () {
    // Logic for handling cell clicks
  },
  onCellDoubleClick: function () {
    /* Logic for handling cell double clicks */
  },
  onCellRightClick: function () {
    /* Logic for handling cell right clicks */
  },
  onCellHoverIn: function () {
    /* Logic for handling cell hover in */
  },
  onCellHoverOut: function () {
    /* Logic for handling cell hover out */
  },
  onDestroy: function () {
    // Logic for destroying the event calendar
  },
  onEventClick: function () {
    // Logic for event click
  },
  onEventCreate: function () {
    // Logic for event create
  },
  onEventCreated: function () {
    // Logic for event created
  },
  onEventCreateFailed: function () {
    // Logic for failed event create
  },
  onEventDelete: function () {
    // Logic for event delete
  },
  onEventDeleted: function () {
    // Logic for event deleted
  },
  onEventDoubleClick: function () {
    // Logic for event double click
  },
  onEventDragStart: function () {
    // Logic for event drag start
  },
  onEventDragEnd: function () {
    // Logic for event drag end
  },
  onEventDragEnter: function () {
    // Logic for event drag enter
  },
  onEventDragLeave: function () {
    // Logic for event drag leave
  },
  onEventHoverIn: function () {
    // Logic for event hover in
  },
  onEventHoverOut: function () {
    // Logic for event hover out
  },
  onEventUpdate: function () {
    // Logic for event update
  },
  onEventUpdated: function () {
    // Logic for event updated
  },
  onEventUpdateFailed: function () {
    // Logic for failed event update
  },
  onEventRightClick: function () {
    // Logic for event right click
  },
  onInit: function () {
    // Logic running on component init
  },
  onLabelClick: function () {
    // Logic for label click
  },
  onPageChange: function () {
    // Your custom event handler goes here
  },
  onPageLoaded: function () {
    // Use it to inject custom markup & attach custom listeners
  },
  onPageLoading: function () {
    // Use it to load data on demand
  },
  onSelectedDateChange: function () {
    // Use it to keep track of the selected date externally
  },
});

mobiscroll.getJson(
  'https://trial.mobiscroll.com/events/?vers=5',
  function (events) {
    inst.setEvents(events);
  },
  'jsonp',
);
```

### HTML

```html
<div mbsc-draggable data-drag-data='{"title": "External drag 1", "color": "#ffdab8"}' class="event-hooks-draggable" style="background: #ffdab8;">
    <div class="draggable-title">External drag 1</div>
    <div class="draggable-text">Drag me to calendar</div>
</div>
<div mbsc-draggable data-drag-data='{"title": "External drag 2", "color": "#ddfcf7"}' class="event-hooks-draggable" style="background: #ddfcf7;">
    <div class="draggable-title">External drag 2</div>
    <div class="draggable-text">Drag me to calendar</div>
</div>
<div id="demo"></div>
```

### CSS

```css
.event-hooks-draggable {
    padding: 10px 20px;
    display: inline-block;
    margin: 15px;
    border-radius: 8px;
    width: 210px;
}

.event-hooks-draggable .draggable-title {
    font-size: 16px;
    font-weight: 600;
}

.event-hooks-draggable .draggable-text {
    opacity: 0.7;
    line-height: 25px;
}

.event-hooks-draggable-1 {
    background: #ffdab8;
}

.event-hooks-draggable-2 {
    background: #ddfcf7;
}
```

## Implementation notes

- Use `view: { calendar: { labels: true } }`. Enable `dragToCreate: true`, `dragToMove: true`, `dragToResize: true`, and `externalDrop: true`.
- Add an `invalid` entry blocking weekends: `{ recurring: { repeat: 'weekly', weekDays: 'SA,SU' } }`.
- **External draggables** - elements outside the calendar can be made draggable into it via `dragData: { title, color }`. React/Vue: use the `Draggable` component with `dragData` and `element` (ref) props. Angular: use the `mbsc-draggable` directive with `[dragData]`. JS/jQuery: use the `mbsc-draggable` attribute with `data-drag-data` as a JSON string.
- Load events from `https://trial.mobiscroll.com/events/?vers=5` via JSONP using `getJson(url, callback, 'jsonp')`. Angular: use `HttpClient.jsonp()` instead for the HTTP call. React/Angular/Vue: assign the fetched array to the state/property bound to the `data` prop. JS/jQuery: call `inst.setEvents(events)` in the callback.
- **All lifecycle hooks** - register every available hook as an empty handler, grouped by concern:
  - **Cell**: `onCellClick`, `onCellDoubleClick`, `onCellRightClick`, `onCellHoverIn`, `onCellHoverOut`
  - **Event interaction**: `onEventClick`, `onEventDoubleClick`, `onEventRightClick`, `onEventHoverIn`, `onEventHoverOut`, `onLabelClick`
  - **Event create/update/delete**: `onEventCreate`, `onEventCreated`, `onEventCreateFailed`, `onEventDelete`, `onEventDeleted`, `onEventUpdate`, `onEventUpdated`, `onEventUpdateFailed`
  - **Event drag**: `onEventDragStart`, `onEventDragEnd`, `onEventDragEnter`, `onEventDragLeave`
  - **Page / lifecycle**: `onInit`, `onDestroy`, `onPageChange`, `onPageLoaded`, `onPageLoading`, `onSelectedDateChange`

## Run it in your project

1. If not signed up yet, or don't own a license, you can [start a free trial](https://mobiscroll.com/starttrial).

2. Clone the repository:

   ```
   git clone https://github.com/acidb/mobiscroll-demos-javascript.git
   ```

3. If not installed yet, install the Mobiscroll CLI from NPM:

   ```
   npm install -g @mobiscroll/cli
   ```

4. Config Mobiscroll - open a terminal window in the root directory and run:

   ```
   mobiscroll config javascript
   ```

5. Install dependencies - open a terminal window in the root directory and run:

   ```
   npm install
   ```

6. Run the project:

   ```
   npm run dev
   ```

On a trial, add `--trial` to the config command: `mobiscroll config javascript --trial`.

Running a demo locally requires a Mobiscroll trial or a license; setup is per framework at https://mobiscroll.com/docs/javascript/getting-started/installation

## Related demos

- Previous: [Gregorian, Jalali & Hijri](https://demo.mobiscroll.com/javascript/eventcalendar/gregorian-jalali-hijri.md) - Show events in Gregorian, Jalali, or Hijri with the Javascript event calendar — switch between calendar systems to support Persian.
