How To Display An Element When A Button Is Clicked In HTML?

It is often required to hide or show a particular element on a web page and display it as soon as the user clicks a button. This blog will discuss how we can achieve this. To enable the display of any element, usually <div> , we use the combination of HTML, CSS and JS altogether.

Creating an HTML Part

Let’s first create an HTML page.

This code presents a very basic html snippet. In this, we have created a button called “Show Heading” and a <div> which we need to hide and show when a button is clicked. 

Also, we have added an onclick attribute which calls the JS function called show().

Creating a CSS Part

It is up to you if you want to use internal CSS or make a separate CSS file and link it with the HTML file.

Since the show is the ID given to the <div> we want to hide it initially, therefore we are initializing its display property to none. Now this <div> will not be displayed when we render this page.

You can add other styling properties as well along with this code. 

Creating the JS part

Lastly, we will be implementing the JS part where we will have to define the function that will be called when the button is clicked.

It is up to you, again, to add this show() function in a separate file along with other JS functions or implement it within the same HTML file with the help of a <script> tag.

This function will be called when the button is clicked. It is first getting the <div> element by referring to its id. Then we are updating its display property to block so that it’s visible on the webpage.

Conclusion

Hence by incorporating these steps, we can dynamically show/hide an element in HTML with the help of CSS and JS. It is a very simple approach to achieve this. Hope you got an idea and this feature will help you in contributing a good interactive UI.


Posted

in

by

Recent Post

  • Generative AI in Hospitality: Integration, Use Cases, Challenges, and Future Outlook

    Generative AI is revolutionizing the hospitality industry, redefining guest experiences, and streamlining operations with intelligent automation. According to market research, the generative AI market in the hospitality sector was valued at USD 16.3 billion in 2023 and is projected to skyrocket to USD 439 billion by 2033, reflecting an impressive CAGR of 40.2% from 2024 […]

  • Generative AI for Contract Management: Overview, Use Cases, Implementation Strategies, and Future Trends

    Effective contract management is a cornerstone of business success, ensuring compliance, operational efficiency, and seamless negotiations. Yet, managing complex agreements across departments often proves daunting, particularly for large organizations. The TalkTo Application, a generative AI-powered platform, redefines contract management by automating and optimizing critical processes, enabling businesses to reduce operational friction and improve financial outcomes. […]

  • Generative AI in customer service: Integration approaches, use cases, best practices, and future outlook

    Introduction The rise of generative AI is revolutionizing customer service, heralding a new era of intelligent, responsive, and personalized customer interactions. As businesses strive to meet evolving customer expectations, these advanced technologies are becoming indispensable for creating dynamic and meaningful engagement. But what does this shift mean for the future of customer relationships? Generative AI […]

  • Generative AI in corporate accounting: Integration, use cases, challenges, ROI evaluation, and future outlook

    Overview Corporate accounting is fundamental to ensuring an organization’s financial stability and strategic growth. As the cornerstone of financial reporting and decision-making, it upholds transparency and accountability in business operations. However, technological advancements, particularly the emergence of generative AI, are redefining the field. By automating repetitive tasks and amplifying data-driven insights, generative AI in corporate […]

  • Generative AI in HR Operations: Overview, Use Cases, Challenges, and Future Trends

    Overview Imagine a workplace where HR tasks aren’t bogged down by endless paperwork or repetitive chores, but instead powered by intelligent systems that think, create, and adapt—welcome to the world of GenAI. Generative AI in HR operations offers a perfect blend of efficiency, personalization, and strategic insight that transforms how organizations interact with their talent. […]

  • Generative AI in Sales: Implementation Approaches, Use Cases, Challenges, Best Practices, and Future Trends

    The world of sales is evolving at lightning speed. Today’s sales teams are not just tasked with meeting ambitious quotas but must also navigate a maze of complex buyer journeys and ever-rising customer expectations. Despite relying on advanced CRM systems and various sales tools, many teams remain bogged down by repetitive administrative tasks, a lack […]

Click to Copy