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>