Skip to content

The PopupTemplate class is used to define and format the content and title of a Layer or Graphic's Popup. You can format the content of the popup's template using a string, popup elements, FieldInfoFormat properties, or a custom function.

This sample demonstrates how to format the content of a PopupTemplate with a custom function.

How it works

When the application starts, the layer's popupTemplate property is set as shown below. The popup template content references the custom function populationChange(), which is defined later in the code.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
        // Create a new popupTemplate for the layer and call the custom populationChange()
        // function to calculate percent change for the county.
        popupTemplate: {
          // autocasts as new PopupTemplate()
          title: "Population in {NAME} County",
          outFields: ["*"],
          content: populationChange,
        },

The population percentage change is calculated and the content will display a red down arrow if the percent rate is negative and a green up arrow if the percent rate is positive. Percentage change formatting is done in the populationChange() function. When the feature is clicked, this function is called. The feature is passed into the function. For this example, the feature attribute field value is POP2013.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
      function populationChange(feature) {
        const div = document.createElement("div");
        const upArrow =
          '<svg width="16" height="16" ><polygon points="14.14 7.07 7.07 0 0 7.07 4.07 7.07 4.07 16 10.07 16 10.07 7.07 14.14 7.07" style="fill:green"/></svg>';
        const downArrow =
          '<svg width="16" height="16"><polygon points="0 8.93 7.07 16 14.14 8.93 10.07 8.93 10.07 0 4.07 0 4.07 8.93 0 8.93" style="fill:red"/></svg>';
        // Calculate the population percent change from 2010 to 2013.
        const diff = feature.graphic.attributes.POP2013 - feature.graphic.attributes.POP2010;
        const pctChange = (diff * 100) / feature.graphic.attributes.POP2010;
        const arrow = diff > 0 ? upArrow : downArrow;
        // Add green arrow if the percent change is positive and a red arrow for negative percent change.
        div.innerHTML = `As of 2010, the total population in this area was <b>${feature.graphic.attributes.POP2010}</b> and the density was <b>${feature.graphic.attributes.POP10_SQMI}</b> sq mi. As of 2013, the total population was <b>${feature.graphic.attributes.POP2013}</b> and the density was <b>${feature.graphic.attributes.POP13_SQMI}</b> sq mi. <br/> <br/>
          Percent change is ${arrow} <span style='color: ${pctChange < 0 ? "red" : "green"};'>${pctChange.toFixed(3)}%</span>`;
        return div;
      }

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.