Replacing multiple characters case insensitively in JavaScript

By using replace() function on any string variable, we can replace character(s) with another character(s). But by default it will only replace the first occurrence. What if we want to replace all occurrence? Here I’ll show you how.

How to truncate a text with JavaScript

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>