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

How to truncate a text with JavaScript

Posted on October 31, 2017August 4, 2020 by Habibie

If we have a long long text and we only want to show our users some part of that text, well we need to truncate that text. In this tutorial I will show you how to do that with JavaScript.

And here is the source code:

<!DOCTYPE html>
<html>
	<head>
		<title>ZK Tutorials</title>
	</head>
	<body>
		<h1>Truncating Text with JavaScript</h1>
		<input id="userinput" placeholder="Type something..."><br>
		<button onclick="truncate()">Truncate</button>
		<script>
			function truncate(){
				var usertext = document.getElementById("userinput").value;
				
				if(usertext.length > 10){
					var tempText = "";
					for(var i = 0; i < 10; i++){
						tempText += usertext[i];
					}
					tempText += " ... [show more]";
					usertext = tempText;
				}
				
				alert(usertext);
			}
		</script>
	</body>
</html>
Post Views: 396
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

  • Hover Reveal script for Unity to show and hide object on mouse hover
  • How to Prevent UI Clicks in Unity from “Bleeding Through” to 3D Objects Behind Them
  • 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
© 2026 ThirteeNov | Powered by Superbs Personal Blog theme