HOW TO CHANGE CSS PROPERTY USING ONCLICK

HOW TO CHANGE CSS PROPERTY USING ONCLICK

Date posted: 16/10/2019

In this tutorial, we will explain how to add CSS classes when click the button using javascript function.

document.getElementById() method used to get the element with the specified id.

<!DOCTYPE html>
<html>
<head>
	<title>JAVA SCRIPT</title>
</head>
<style type="text/css">
	
	  .change_red{ 
	  	       font-size: 20px;
               color: white;
	  	       width: 13pc;
	  	       height: 2pc;
                background-color: red; 
                padding:5px; 
                border:1px solid black; 
                border-radius:3px; 
              } 
      .change_green{ 
               font-size: 20px;
                color: white;
      	        width: 13pc;
                height: 2pc;
                background-color: green; 
                padding:5px; 
                border:1px solid black; 
                border-radius:3px; 
               } </style>
<body>
<center>
	<p>JAVA SCRIPT</p>
	<button class="change_red" onclick="myFunction()" id="mybutton">OK</button>
</center>
</body>




	function myFunction() 
	{
		document.getElementById('mybutton').className = "change_green";
    }





</html>

Before clicking the “OK” button:

After clicking the “OK” button:

Thanks for using phoenix solutions.

admin

Our team has expertise across software and web development, WordPress, e-commerce, mobile applications, UI/UX, cloud and infrastructure, DevOps, CI/CD, API integration, security, testing, automation, and technical support. The team also works with AI-based software solutions, LLMs, AI workflows, AI agents, and intelligent application development to help businesses automate processes and build smarter digital solutions. We focus on developing, deploying, maintaining, and optimising secure, scalable, and reliable technology solutions while helping businesses adopt modern technologies and drive digital transformation.

Leave a Reply

Scroll to Top