---
title: Use Panels to Group Related Questions
product: Form Library
description: A panel is a section within a form that contains multiple questions. By adding panels into your surveys or forms, you can effectively group related questions together. This enables you to conveniently modify the settings of multiple questions at once. Explore this free demo for JavaScript to learn more.
framework: React
source: https://surveyjs.io/form-library/examples/set-properties-on-multiple-questions-using-panel/reactjs
index: https://surveyjs.io/form-library/examples/overview.md
---

# Use Panels to Group Related Questions (React)

If your survey or form contains related questions, you can organize them in groups to give respondents context and help them better understand the purpose of each question. A survey element that groups other survey elements on a page is Panel. This demo shows how to create and configure panels.

## Create a Panel

To add a panel to your survey or form, define an object with the `type` property set to `"panel"` and add it to the [`elements`](https://surveyjs.io/form-library/documentation/pagemodel#elements) array. Within this object, specify the panel's [`title`](https://surveyjs.io/form-library/documentation/api-reference/panel-model#title) and a unique [`name`](https://surveyjs.io/form-library/documentation/api-reference/panel-model#name) that identifies the panel. Optionally, you can specify a [`description`](https://surveyjs.io/form-library/documentation/api-reference/panel-model#description) to place under the `title`.

To add questions to a panel, declare a nested [`elements`](https://surveyjs.io/form-library/documentation/api-reference/panel-model#elements) array and populate it with question configurations. Since panels can be nested one into another, the `elements` array can also contain configurations of other panels.

## Expand and Collapse Panels

Panels can be expanded and collapsed in code or in the UI. When collapsed, a panel displays only its `title` and `description`. To expand or collapse a panel, users should click its title. This feature helps you optimize screen space when your survey contains many questions.

To expand or collapse a panel, call its [`expand()`](https://surveyjs.io/form-library/documentation/api-reference/panel-model#expand) or [`collapse()`](https://surveyjs.io/form-library/documentation/api-reference/panel-model#collapse) method. To find out the current state of a panel and decide which method to call, use the [`isExpanded`](https://surveyjs.io/form-library/documentation/api-reference/panel-model#isExpanded) and [`isCollapsed`](https://surveyjs.io/form-library/documentation/api-reference/panel-model#isCollapsed) properties. Alternatively, you can call the [`toggleState()`](https://surveyjs.io/form-library/documentation/api-reference/panel-model#toggleState) method to toggle a panel's expand state. All these methods and properties belong to a [`PanelModel`](https://surveyjs.io/form-library/documentation/api-reference/panel-model) object instance. To access it, use [`SurveyModel`](https://surveyjs.io/form-library/documentation/api-reference/survey-data-model)'s [`getPanelByName()`](https://surveyjs.io/form-library/documentation/api-reference/survey-data-model#getPanelByName) method:

```js
import { Model } from "survey-core";

const surveyJson = {
  // ... 
  "elements": [{
    "type": "panel",
    "name": "myPanel",
    "elements": [ ... ]
  }]
};
const survey = new Model(surveyJson);
// Access a panel with a specified name
const myPanel = survey.getPanelByName("myPanel");
// Call `toggleState()` or any other `PanelModel` method
myPanel.toggleState();
```

To allow users to expand and collapse a panel, set its [`state`](https://surveyjs.io/form-library/documentation/api-reference/panel-model#state) property to `"expanded"` or `"collapsed"`, depending on the initial state you want. In this demo, you can expand and collapse both panels.

## Files

### `public/index.html`

```html
<div id="surveyElement" style="position: absolute; top: 0; left: 0; right: 0; bottom: 0; min-height: 100%; height:100%"></div>
```

### `src/SurveyComponent.jsx`

```js
import React from "react";
import { Model } from "survey-core";
import { Survey } from "survey-react-ui";
import "survey-core/survey-core.min.css";
import "./index.css";
import { json } from "./json";

function SurveyComponent() {
    const survey = new Model(json);
    survey.onComplete.add((sender, options) => {
        console.log(JSON.stringify(sender.data, null, 3));
    });
    return (<Survey model={survey} />);
}

export default SurveyComponent;
```

### `src/index.css`

```css
/* You can add your custom CSS here. */
```

### `src/index.js`

```js
import React from "react";
import { createRoot } from "react-dom/client";
import SurveyComponent from "./SurveyComponent";

const root = createRoot(document.getElementById("surveyElement"));
root.render(<SurveyComponent />);
```

### `src/json.js`

```js
export const json = {
  "pages": [
    {
      "name": "page1",
      "elements": [
        {
          "type": "panel",
          "name": "personal-information",
          "title": "Personal Information",
          "state": "expanded",
          "elements": [
            {
              "type": "text",
              "name": "first-name",
              "title": "First name",
              "isRequired": true
            },
            {
              "type": "text",
              "name": "last-name",
              "startWithNewLine": false,
              "title": "Last name",
              "isRequired": true
            },
            {
              "type": "text",
              "name": "birthdate",
              "title": "Birthdate",
              "isRequired": true,
              "inputType": "date",
              "autocomplete": "bday",
              "maxValueExpression": "today()"
            },
            {
              "type": "dropdown",
              "name": "country",
              "startWithNewLine": false,
              "title": "Country",
              "choicesByUrl": {
                "url": "https://surveyjs.io/api/CountriesExample",
                "valueName": "name"
              }
            }
          ]
        },
        {
          "type": "panel",
          "name": "contact-information",
          "title": "Contact Information",
          "state": "expanded",
          "elements": [
            {
              "type": "text",
              "name": "email",
              "title": "E-mail address",
              "inputType": "email",
              "autocomplete": "email"
            },
            {
              "type": "text",
              "name": "phone",
              "startWithNewLine": false,
              "title": "Phone number",
              "inputType": "tel",
              "autocomplete": "tel"
            },
            {
              "type": "text",
              "name": "skype",
              "startWithNewLine": false,
              "title": "Skype"
            }
          ]
        }
      ]
    }
  ],
  "questionErrorLocation": "bottom"
};
```

### `src/theme.js`

```js
export const themeJson = {};
```

### `package.json`

```json
{
  "dependencies": {
    "react": "latest",
    "react-dom": "latest",
    "survey-core": "latest",
    "survey-react-ui": "latest"
  },
  "devDependencies": {
    "react-scripts": "latest"
  }
}
```

## Other Frameworks

- [Angular](https://surveyjs.io/form-library/examples/set-properties-on-multiple-questions-using-panel/angular.md)
- [Vue 3](https://surveyjs.io/form-library/examples/set-properties-on-multiple-questions-using-panel/vue3js.md)
- [jQuery](https://surveyjs.io/form-library/examples/set-properties-on-multiple-questions-using-panel/jquery.md)
- [Vanilla JS](https://surveyjs.io/form-library/examples/set-properties-on-multiple-questions-using-panel/vanillajs.md)
