Having trouble getting help?
Contact Support
Contact Support
Expand/Collapse the accordion only on icon click in EJ2 TypeScript Accordion control
17 Dec 20243 minutes to read
You can restrict the expansion and collapse of an Accordion item to occur only when the toggle icon is clicked. This can be achieved by using the Accordion’s expanding
event and binding a click
event specifically to the toggle icon.
By intercepting the expanding
event, you can control when the Accordion items should expand or collapse based on user interactions. Using args.cancel = true
, you can prevent the default behavior unless the toggle icon itself is clicked.
import { Accordion } from '@syncfusion/ej2-navigations';
import { enableRipple } from '@syncfusion/ej2-base';
enableRipple(true);
let istoggleIconClicked = false;
let accordion: Accordion = new Accordion({
expandMode: 'Single',
expanding: onExpanding,
items: [
{ header: 'ASP.NET', content: 'Microsoft ASP.NET is a set of technologies in the Microsoft .NET Framework for building Web applications and XML Web services. ASP.NET pages execute on the server and generate markup such as HTML, WML, or XML that is sent to a desktop or mobile browser. ASP.NET pages use a compiled,event-driven programming model that improves performance and enables the separation of application logic and user interface.' },
{ header: 'ASP.NET MVC', content: 'The Model-View-Controller (MVC) architectural pattern separates an application into three main components: the model, the view, and the controller. The ASP.NET MVC framework provides an alternative to the ASP.NET Web Forms pattern for creating Web applications. The ASP.NET MVC framework is a lightweight, highly testable presentation framework that (as with Web Forms-based applications) is integrated with existing ASP.NET features, such as master pages and membership-based authentication.' },
{ header: 'JavaScript', content: 'JavaScript (JS) is an interpreted computer programming language.It was originally implemented as part of web browsers so that client-side scripts could interact with the user, control the browser, communicate asynchronously, and alter the document content that was displayed.More recently, however, it has become common in both game development and the creation of desktop applications.' },
]
});
accordion.appendTo('#element');
function onExpanding(args) {
if (!istoggleIconClicked) {
args.cancel = true;
}
else {
istoggleIconClicked = false;
}
}
const toggleIcons = document.querySelectorAll('.e-toggle-icon');
toggleIcons.forEach((toggleIcon) => {
toggleIcon.addEventListener('click', () => {
istoggleIconClicked = true;
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 Accordion</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript Toolbar Controls" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/material.css" rel="stylesheet" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>LOADING....</div>
<div id='container'>
<div id='element'></div>
</div>
</body>
</html>