Skip to content

ThirteeNov

My personal blog about coding and internet

Menu
  • About me
  • About Zofia Kreasi
  • Cart
  • Checkout
  • Making an airplane game from scratch in Unity
  • My account
  • Privacy Policy
  • Privacy Policy – zkLeaderboard
  • Sample Page
  • Shop
  • Tutorials on Learning JavaScript
  • ZKAccounts – Privacy Policy
Menu

Creating JavaScript toggle button

Posted on February 7, 2017August 4, 2020 by Habibie

In this video I will show how simple is to make JavaScript toggle button to hide or show an element on HTML document.

Here is the source codes:

<!DOCTYPE html>
<html>
	<head>
		<title>Toggle Button</title>
	</head>
	<body>
		<button onclick="toggle()">Toggle</button>
		<p id="test">Sample Text</p>
		<script>
			var toggled = false;
			function toggle(){
				if(!toggled){
					toggled = true;
					document.getElementById("test").style.display = "none";
					return;
				}
				if(toggled){
					toggled = false;
					document.getElementById("test").style.display = "block";
					return;
				}
			}
		</script>
	</body>
</html>

Here is the live demo of the script:

Sample Text

Or, you can use jQuery to make this toggle thing much easier. Read this tutorial.

Post Views: 211
ciihuy2020

Welcome!

  • My YouTube Channel
  • My GitHub Page
  • About me

Categories

  • 3DVista
  • Android
  • Apache
  • C#
  • Cordova
  • Electron & Node JS
  • HTML5, CSS & JavaScript
  • iOS
  • Let's Make Unity Games
  • Misc
  • Photoshop
  • PHP
  • Python
  • Uncategorized
  • Unity
  • WordPress

Recent Posts

  • Make objects like wires and cables easily in Unity using Ciihuy Curved Mesh
  • [SOLVED] Can’t Add Custom Domain to Blogger After Losing CNAME Verification
  • iOS App Icon Generator by CiihuyCom
  • Advanced Blinking Marker Script to show objects position in your game canvas
  • Ciihuy Images Merger – Fast & Easy Online Image Combiner
© 2025 ThirteeNov | Powered by Superbs Personal Blog theme