Quick Overview of JavaScript Button in Essential JS 2 | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (174).NET Core  (29).NET MAUI  (207)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (215)BoldSign  (14)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (66)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (100)Streamlit  (1)Succinctly series  (131)Syncfusion  (915)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (36)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (147)Chart  (131)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (628)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (40)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (507)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (10)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (592)What's new  (332)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)

Quick Overview of JavaScript Button in Essential JS 2

The Essential JS 2 button component is an advanced version of an HTML button. It is the best alternative for an HTML button since it has all the core functionalities of an HTML button plus advanced features such as different types and predefined styles that are adaptable to different application environments.

The Essential JS 2 button can contain text, icons, or both. The button can also be used to communicate with users through colors, shapes, and images without any text. For example, a button with an image of a magnifying glass almost always represents a search option. A green button almost always represents a successful action, such as a GitHub merge.

This blog post will provide an overview of the Essential JS 2 button, how to configure it, and how to use its advanced features.

Getting started

You need to set up the development environment before creating a button. It is very easy to get started with the button since its source code is available in GitHub and packages are available in npm.

To get started with the button component, you can clone the Essential JS 2 quick-start project using the following commands.

git clone https://github.com/syncfusion/ej2-quickstart.git quickstart
cd quickstart
npm install

The project is preconfigured with common settings (i.e. src/styles/styles.css and system.config.js files) for starting all the Essential JS 2 components.

Configuring the button

Now all configuration related to the environment is completed. Next, we need to configure the button in an application. Add the HTML button tag with an id attribute set as element to your index.html file.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Essential JS 2</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
    <meta name="description" content="Essential JS 2" />
    <meta name="author" content="Syncfusion" />
    <link rel="shortcut icon" href="resources/favicon.ico" />
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
    <!--style reference from node_modules/@syncfusion/ej2/-->
    <link href="/styles/styles.css" rel="stylesheet" />
    <script src="node_modules/systemjs/dist/system.src.js" type="text/javascript"></script>
    <script src="system.config.js" type="text/javascript"></script>
</head>
<body>
    <div>
        <!--Element that is going to be rendered-->
        <button id="element">Button</button>
    </div>
</body>
</html>

Now let’s initialize the button component in your app.ts file and append it to #element as shown in following code example.

import { Button } from '@syncfusion/ej2-buttons';

// Initialize button component.
let button: Button = new Button();

// Render initialized button.
button.appendTo('#element');
npm start

Run the application in the browser using the previous command to render the button. It should appear as follows.

JavaScript Button

Button component

Themes give components a look and feel that fits the application environment. The Essential JS 2 button supports the following themes:

  • Material
  • Office 365
  • Bootstrap
  • High contrast

In this demo application, the Material theme will be used for the button.

 

Defining button styles

The button has a predefined set of styles. Each style serves its own purpose. You can style the button by simply adding a class name through its interface. For this demo we are going to create several buttons and style them through classes.

<button id="primarybtn">Primary</button>
<button id="successbtn">Success</button>
<button id="infobtn">Info</button>
<button id="warningbtn">Warning</button>
<button id="dangerbtn">Danger</button>
<button id="linkbtn">Link</button>
import { Button } from '@syncfusion/ej2-buttons';
//Primary Button - Used to represent a primary action.
let button: Button = new Button({ cssClass: `e-primary`}, '#primarybtn');
//Success Button - Used to represent a positive action.
button = new Button({ cssClass: `e-success`}, '#successbtn');
//Info Button - Used to represent an informative action.
button = new Button({ cssClass: `e-info`}, '#infobtn');
//Warning Button - Used to represent an action with caution.
button = new Button({ cssClass: `e-warning`}, '#warningbtn');
//Danger Button - Used to represent a negative action.
button = new Button({ cssClass: `e-danger`}, '#dangerbtn');
//Link Button - Changes the appearance of the Button like a hyperlink.
button = new Button({ cssClass: `e-link`}, '#linkbtn');

Predefined Button Styles

Button components with styles 

Creating an icon button

Users tend to perceive visual information better than text. The button can include an icon to add a visual representation of its action. Icons can be added to buttons through their interface. You can customize the icon position to either the left or right side of the button. Icon-only buttons are also supported. In this demo we are going to create a button and load it with different icons.

<style>
 /*For stop icon button*/
        .e-stop-icon::before {
            content: '\e790';
        }
        /*For play icon button*/
        .e-play-icon::before {
            content: '\e798';
        }
        /*For open icon button*/
        .e-open-icon::before {
            content: '\e782';
        }
</style>
<button id= 'iconbutton'></button>
<button id= 'iconleftbtn'>Play</button>
<button id= 'iconrightbtn'>Stop</button>
import { Button } from '@syncfusion/ej2-buttons';

let button: Button = new Button({iconCss: 'e-icons e-open-icon'}, '#iconbutton');

button = new Button({iconCss: 'e-icons e-play-icon', iconPosition: 'Right'}, '#iconleftbtn');

button = new Button({iconCss: 'e-icons e-stop-icon'}, '#iconrightbtn');

Icon Button

Icon-only button (top), button with icon on left (center), button with icon on right (bottom)

Customization

Buttons also have options for customizing their size, shape, and functionalities:

  • A button’s shape can be customized as a round button, block button, or outline button.
  • Two different sizes are available.
  •  A button can be extended as a toggle button or repeat button by handling event handlers.

In this demo we are going to create different buttons and customize their CSS.

<style>
/*For round add icon button*/
		.e-add:before {
            content: '\e823';
        }
        .block {
            margin: 40px 0px;
        }
        .block button {
            margin-right: 15px;
        }
</style>
<div class="block">
            <div class="col-xs-6 col-sm-4 col-lg-4 col-md-4 ">
                <div class="block">
                    <button id='outlinebtn'></button>
                </div>
                <div class="block">
                    <button id='roundbtn'></button>
                </div>
            </div>
            <div class="col-xs-6 col-sm-4 col-lg-4 col-md-4 ">
                <div class="block">
                    <button id='smallbtn'>Small</button>
                </div>
                <div class="block">
                    <button id='normalbtn'>Normal</button>
                </div>
            </div>
            <div class="col-xs-12 col-sm-4 col-lg-4 col-md-4 ">
                <div class="block">
                    <button id="blockbtn">Block Button</button>
                </div>
                <div class="block">
                    <button id="primaryblockbtn">Block Button</button>
                </div>
            </div>
</div>
import { Button } from '@syncfusion/ej2-buttons';
let button: Button =new Button({cssClass:'e-outline e-primary', content: 'outline'}, '#outlinebtn');
button = new Button({cssClass:'e-round e-primary', iconCss: 'e-icons e-add'}, '#roundbtn');
button = new Button({cssClass: `e-block`});
button.appendTo('#blockbtn');
button = new Button({ isPrimary: true, cssClass: `e-block` });
button.appendTo('#primaryblockbtn');
button = new Button({ cssClass: `e-small`}, '#smallbtn');
button = new Button();
button.appendTo('#normalbtn');

 

Button types

Buttons with different shapes, sizes, and behaviors

 Conclusion

In this blog, we have looked at how to create a simple button, change its style, and customize its UI and behavior through its properties and CSS. You can download the button sample on GitHub that has been prepared for this blog.

Feel free to view our interactive samples and documentation to explore the other features of the button. If you have any questions or require clarification, please let us know in the comments section below. You can also contact us through our support forum or Direct-Trac. We are happy to assist you!

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed

Table of Contents