Getting Started with EJ2 JavaScript Radio Button

23 Nov 20267 minutes to read

This section explains the steps required to create a simple Essential® JS 2 Radio button and demonstrate the basic usage of the Radio button control in a JavaScript application.

Dependencies

The list of dependencies required to use the Radio button component in your application is given below:

|-- @syncfusion/ej2-buttons
    |-- @syncfusion/ej2-base

Setup for local environment

Refer to the following steps to set up your local environment.

Step 1: Create a root folder named my-app for your application.

Step 2: Create a my-app/resources folder to store local scripts and styles files.

Step 3: Open Visual Studio Code and create my-app/index.js and my-app/index.html files to initialize the Essential® JS 2 Radio button control.

Control Initialization

The Essential® JS 2 JavaScript controls can be initialized by using below css styles and scripts

Add Syncfusion® RadioButton CSS Styles and Scripts

Include the following CSS and JavaScript files from CDN in the <head> section of the index.html file. The ej2-base scripts are required dependency packages for the RadioButton component.

Styles(CSS):

https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/radio-button/index.css

Scripts (javascript):

https://cdn.syncfusion.com/ej2/35.1.37/ej2-base/dist/global/ej2-base.min.js
https://cdn.syncfusion.com/ej2/35.1.37/ej2-buttons/dist/global/ej2-buttons.min.js

Adding Radio Button component to the application

Add the RadioButton element and initiate the Syncfusion® Javascript RadioButton control in the index.html by using following code.

<!DOCTYPE html>
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
                
        <title>Essential JS 2</title>

        <!-- Essential JS 2 Base's bootstrap5.3 theme (Dependency Styles) -->
         <link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/radio-button/index.css" rel="stylesheet" />

        <!-- Essential JS 2 Base's global script (Dependency Script) -->
        <script src="https://cdn.syncfusion.com/ej2/35.2.12/ej2-base/dist/global/ej2-base.min.js" type="text/javascript"></script>

        <!-- Essential JS 2 RadioButton's global script (Control Script) -->
        <script src="https://cdn.syncfusion.com/ej2/35.2.12/ej2-buttons/dist/global/ej2-buttons.min.js" type="text/javascript"></script>

   </head>
   <body>
       <!--element which is going to render-->
        <input id="element1" type="radio"/>
        <input id="element2" type="radio"/>
        <script>
          ej.base.enableRipple(true);

          // Initialize RadioButton control.
          var radiobutton = new ej.buttons.RadioButton({ label: 'Option 1', name: 'default'});
          // Render initialized RadioButton.
          radiobutton.appendTo('#element1');

          radiobutton = new ej.buttons.RadioButton({ label: 'Option 2', name: 'default'});
          radiobutton.appendTo('#element2');
        </script>
   </body>
  </html>
#container {
    visibility: hidden;
}

#loader {
  color: #008cff;
  height: 40px;
  left: 45%;
  position: absolute;
  top: 45%;
  width: 30%;
}

.e-radio-wrapper {
  margin-top: 18px;
}

li {
  list-style: none;
}

Change the RadioButton state

The Essential® JS 2 RadioButton contains 2 states visually, they are as follows:

  • Checked
  • Unchecked

The RadioButton checked property is used to handle the checked and unchecked state. In the checked state an inner circle will be added to the visualization of RadioButton.

<!DOCTYPE html>
  <html xmlns="http://www.w3.org/1999/xhtml">
   <head>
                
        <title>Essential JS 2</title>

        <!-- Essential JS 2 Base's bootstrap5.3 theme (Dependency Styles) -->
        <link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/radio-button/index.css" rel="stylesheet" />

        <!-- Essential JS 2 Base's global script (Dependency Script) -->
        <script src="https://cdn.syncfusion.com/ej2/35.2.12/ej2-base/dist/global/ej2-base.min.js" type="text/javascript"></script>

        <!-- Essential JS 2 RadioButton's global script (Control Script) -->
        <script src="https://cdn.syncfusion.com/ej2/35.2.12/ej2-buttons/dist/global/ej2-buttons.min.js" type="text/javascript"></script>

        <link href="styles.css" rel="stylesheet">
   </head>
   <body>
       <!--element which is going to render-->
        <input type="radio" id="radiobutton1"/>
        <input type="radio" id="radiobutton2"/>
        <script>
            ej.base.enableRipple(true);

            //checked state.
            var radiobutton = new ej.buttons.RadioButton({ label: 'Option 1', name: 'state', checked: true });
            radiobutton.appendTo('#radiobutton1');

            //unchecked state.
            radiobutton = new ej.buttons.RadioButton({ label: 'Option 2', name: 'state' });
            radiobutton.appendTo('#radiobutton2');
        </script>
   </body>
  </html>
#container {
    visibility: hidden;
}

#loader {
  color: #008cff;
  height: 40px;
  left: 45%;
  position: absolute;
  top: 45%;
  width: 30%;
}

.e-radio-wrapper {
  margin-top: 18px;
}

li {
  list-style: none;
}

See Also