[GIS] How to resize an infoWindow dynamically, based on the contents

arcgis-javascript-api

How can I set the size of an infoWindow dynamically, based on the length of its Title, when the Title isn't known until the user clicks on a map feature?

Take the map here as an example – how could you resize the popup infoWindow to accommodate varying Title lengths?

This map uses the ${Name} notation to represent the Title field, with the field value substituted into the infoWindow when the feature is clicked.

The infoWindow has a fixed size (set using map.infoWindow.resize(250,95)). Since the length of the title isn't known until the feature is clicked, how is it possible to resize the infoWindow based on the length of the Title?

Using ArcGIS Server 10 with the JavaScript API 2.3

Best Answer

Here is a jsFiddle example. Try clicking on Ohio for a smaller info window and then South Carolina for a larger info window.

The relevant code is below:

var g = evt.graphic;
map.infoWindow.setContent(g.getContent());
if(g.getTitle().length > 10){
  map.infoWindow.resize(500,500);
} else {
  map.infoWindow.resize(100,100);
}