truncate string using jquery

so lets say we had the following string on the page and we needed to make it a bit shorter for some reason. like if you dont have control of the code base...

Test String with some random text

grab the text and add it to a variable

var text = $('#text').html(); console.log(text);

now you need to count the number of characters you want to split off this text, so the 5 will be where it starts to grab the string and the 11 is where it stops.

var truncated = text.substring(5, 11); console.log(truncated);

this should show String in the test box

Test Area 1

var text = $('#text').html(); var truncated = text.substring(5, 11); $('#test1').html(truncated);

now lets append something to the end of it

Test Area 2

function test2() {
		var text = $('#text').html(); 
		var truncated = text.substring(5, 11) . "<span style='color:#0F0;'>..<span>"; 
		$('#test2').html(truncated).css("color", "#F00");

Other Items in js
Related Search Terms
Page Views

This page has been viewed 341 times.

Search Code
Search Code by entering your search text above.

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.


Random Quote
1. Show your work to the world instead of keeping in your head💆. 2. Do the work consistently👌 3. Respect your work🥰 4. Don't postpone your work 5. Make mistakes 🔥🔥🔥