---
title: "Attach a popup to a marker instance | JavaScript maps SDK"
description: "Attach and display a popup to a map marker."
---

> For AI agents: a documentation index is available at [/llms.txt](https://docs.maptiler.com/llms.txt). Markdown versions of all pages are available by appending .md to any URL.

# Attach a popup to a marker instance

Attach a [`Popup`](https://docs.maptiler.com/sdk-js/api/markers/#popup) to a [Marker](https://docs.maptiler.com/sdk-js/api/markers/#marker) and display it when the user clicks on the Marker.

### NPM module setup

```bash
npm install --save @maptiler/sdk
```

#### main.js

```js
import { Map, MapStyle, config, Popup } from '@maptiler/sdk';
import '@maptiler/sdk/dist/maptiler-sdk.css';

config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
const monument = [-77.0353, 38.8895];
const map = new Map({
    container: 'map',
    style: MapStyle.STREETS, 
    center: monument,
    zoom: 15
});

// create the popup
const popup = new Popup({ offset: 25 }).setText(
    'Construction on the Washington Monument began in 1848.'
);

// create DOM element for the marker
const el = document.createElement('div');
el.id = 'marker';

// create the marker
new maptilersdk.Marker({element: el})
    .setLngLat(monument)
    .setPopup(popup) // sets a popup on this marker
    .addTo(map);
```

#### index.html

```html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="robots" content="noindex">
  <title>Attach a popup to a marker instance</title>
  <link rel="stylesheet" href="./style.css">
</head>
<body>
  <div id="map"></div>
  <script type="module" src="./main.js"></script>
</body>
</html>
```

#### style.css

```css
body {margin: 0; padding: 0;}
#map {position: absolute; top: 0; bottom: 0; width: 100%;}
#marker {
  background-image: url('https://docs.maptiler.com/sdk-js/assets/washington-monument.jpg');
  background-size: cover;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  cursor: pointer;
}

.maplibregl-popup {
  max-width: 200px;
}
```

### Basic JavaScript setup

```html
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Attach a popup to a marker instance</title>
<script src="https://cdn.maptiler.com/maptiler-sdk-js/v4.2.0/maptiler-sdk.umd.min.js"></script>
<link href="https://cdn.maptiler.com/maptiler-sdk-js/v4.2.0/maptiler-sdk.css" rel="stylesheet" />
<style>
  body {margin: 0; padding: 0;}
  #map {position: absolute; top: 0; bottom: 0; width: 100%;}
  #marker {
    background-image: url('https://docs.maptiler.com/sdk-js/assets/washington-monument.jpg');
    background-size: cover;
    width: 50px;
    height: 50px;
    border-radius: 50%;
    cursor: pointer;
  }

  .maplibregl-popup {
    max-width: 200px;
  }
</style>
</head>
<body>
<div id="map"></div>

<script>
    maptilersdk.config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
    const monument = [-77.0353, 38.8895];
    const map = new maptilersdk.Map({
        container: 'map',
        style: maptilersdk.MapStyle.STREETS, 
        center: monument,
        zoom: 15
    });

    // create the popup
    const popup = new maptilersdk.Popup({ offset: 25 }).setText(
        'Construction on the Washington Monument began in 1848.'
    );

    // create DOM element for the marker
    const el = document.createElement('div');
    el.id = 'marker';

    // create the marker
    new maptilersdk.Marker({element: el})
        .setLngLat(monument)
        .setPopup(popup) // sets a popup on this marker
        .addTo(map);
</script>
</body>
</html>
```

## Related examples

### [Display a popup on hover](https://docs.maptiler.com/sdk-js/examples/popup-on-hover/)

Examples

Show a popup when hovering over a custom marker.

### [Show polygon information on click](https://docs.maptiler.com/sdk-js/examples/polygon-popup-on-click/)

Examples

Display a popup when a user clicks a polygon.

### [Weather custom popup](https://docs.maptiler.com/sdk-js/examples/weather-custom-popup/)

Examples

Create highly customizable popups for weather maps using MarkerLayout.

### [Display a popup](https://docs.maptiler.com/sdk-js/examples/popup/)

Examples

Add an information popup to the map.
