How to toggle Truncate text in JavaScript?
up vote
3
down vote
favorite
Example here:

How to toggle text from 'truncate' to 'full text'?
like i want to toggle full text when a person clicks on 'read more' button and also hide text when 'hide text' button is clicked
Snippet:
var textHolder = document.querySelector('.demo');
var btn = document.querySelector('.btn');
function Truancate(textHolder, limit) {
let txt = textHolder.innerHTML;
if (txt.length > limit) {
let newText = txt.substr(0, limit) + ' ...';
textHolder.innerHTML = newText;
}
}
Truancate(textHolder, textHolder.offsetWidth / 10);
function toggleText() {
// here i want to show full text...
// and also -> btn.innerHTML = 'Hide Text' | 'Show Text;
}
btn.addEventListener('click', toggleText);<section class="demo" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn">Read More</button>javascript html5 css3
add a comment |
up vote
3
down vote
favorite
Example here:

How to toggle text from 'truncate' to 'full text'?
like i want to toggle full text when a person clicks on 'read more' button and also hide text when 'hide text' button is clicked
Snippet:
var textHolder = document.querySelector('.demo');
var btn = document.querySelector('.btn');
function Truancate(textHolder, limit) {
let txt = textHolder.innerHTML;
if (txt.length > limit) {
let newText = txt.substr(0, limit) + ' ...';
textHolder.innerHTML = newText;
}
}
Truancate(textHolder, textHolder.offsetWidth / 10);
function toggleText() {
// here i want to show full text...
// and also -> btn.innerHTML = 'Hide Text' | 'Show Text;
}
btn.addEventListener('click', toggleText);<section class="demo" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn">Read More</button>javascript html5 css3
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
Example here:

How to toggle text from 'truncate' to 'full text'?
like i want to toggle full text when a person clicks on 'read more' button and also hide text when 'hide text' button is clicked
Snippet:
var textHolder = document.querySelector('.demo');
var btn = document.querySelector('.btn');
function Truancate(textHolder, limit) {
let txt = textHolder.innerHTML;
if (txt.length > limit) {
let newText = txt.substr(0, limit) + ' ...';
textHolder.innerHTML = newText;
}
}
Truancate(textHolder, textHolder.offsetWidth / 10);
function toggleText() {
// here i want to show full text...
// and also -> btn.innerHTML = 'Hide Text' | 'Show Text;
}
btn.addEventListener('click', toggleText);<section class="demo" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn">Read More</button>javascript html5 css3
Example here:

How to toggle text from 'truncate' to 'full text'?
like i want to toggle full text when a person clicks on 'read more' button and also hide text when 'hide text' button is clicked
Snippet:
var textHolder = document.querySelector('.demo');
var btn = document.querySelector('.btn');
function Truancate(textHolder, limit) {
let txt = textHolder.innerHTML;
if (txt.length > limit) {
let newText = txt.substr(0, limit) + ' ...';
textHolder.innerHTML = newText;
}
}
Truancate(textHolder, textHolder.offsetWidth / 10);
function toggleText() {
// here i want to show full text...
// and also -> btn.innerHTML = 'Hide Text' | 'Show Text;
}
btn.addEventListener('click', toggleText);<section class="demo" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn">Read More</button>var textHolder = document.querySelector('.demo');
var btn = document.querySelector('.btn');
function Truancate(textHolder, limit) {
let txt = textHolder.innerHTML;
if (txt.length > limit) {
let newText = txt.substr(0, limit) + ' ...';
textHolder.innerHTML = newText;
}
}
Truancate(textHolder, textHolder.offsetWidth / 10);
function toggleText() {
// here i want to show full text...
// and also -> btn.innerHTML = 'Hide Text' | 'Show Text;
}
btn.addEventListener('click', toggleText);<section class="demo" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn">Read More</button>var textHolder = document.querySelector('.demo');
var btn = document.querySelector('.btn');
function Truancate(textHolder, limit) {
let txt = textHolder.innerHTML;
if (txt.length > limit) {
let newText = txt.substr(0, limit) + ' ...';
textHolder.innerHTML = newText;
}
}
Truancate(textHolder, textHolder.offsetWidth / 10);
function toggleText() {
// here i want to show full text...
// and also -> btn.innerHTML = 'Hide Text' | 'Show Text;
}
btn.addEventListener('click', toggleText);<section class="demo" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn">Read More</button>javascript html5 css3
javascript html5 css3
edited Nov 20 at 5:07
S.Serpooshan
3,93821637
3,93821637
asked Nov 20 at 4:54
TheV
387
387
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
3
down vote
You don't need javascript for this. a simple css text-overflow: ellipsis; can do the trick.
It is a better/standard way to truncate the long text because it will truncate the text display at exact position which is depend on the font-size, width of parent container, etc... that is not possible simply with js. here is a demo:
var textHolder = document.querySelector('.demo');
var btn = document.querySelector('.btn');
function toggleText() {
textHolder.classList.toggle("truncate");
}
btn.addEventListener('click', toggleText);
toggleText(); //to truncate at first time.truncate {
text-overflow: ellipsis;
/*BOTH of the following are required for text-overflow (overflow: hidden; and white-space: nowrap;)*/
overflow: hidden;
white-space: nowrap;
}<section class="demo" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn">Read More</button>Note that cutting innerHTML can be dangerous as you may cut at improper position and corrupt the HTML code opening/closing tags...
2
a better answer indeed :)
– Towkir Ahmed
Nov 20 at 5:14
add a comment |
up vote
1
down vote
First you need to store the full text to a variable, never change that variable, then store the truncated text into another variable and finally toggle between those to variable value to set the text to your target element.
Here is the sinppet:
var textHolder = document.querySelector('.demo');
var fullText = textHolder.innerHTML;
var btn = document.querySelector('.btn');
var textStatus = 'full'; // use this to check the status of text and toggle;
function Truancate(textHolder, limit) {
let txt = textHolder.innerHTML;
if (txt.length > limit) {
let newText = txt.substr(0, limit) + ' ...';
textHolder.innerHTML = newText;
textStatus = 'truncated';
}
}
Truancate(textHolder, textHolder.offsetWidth / 10);
function toggleText() {
// here i want to show full text...
// and also -> btn.innerHTML = 'Hide Text' | 'Show Text;
if (textStatus === 'truncated') {
textHolder.innerHTML = fullText;
textStatus = 'full';
} else {
Truancate(textHolder, textHolder.offsetWidth / 10);
}
}
btn.addEventListener('click', toggleText);<section class="demo" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn">Read More</button>add a comment |
up vote
0
down vote
Here is some more object oriented way and can be used anywhere in like situation
What you need to do is only follow following step
Create a wrapper container with an id let' say
readMoreContainerAdd a class named
readMoreTextto the section containing the text to toggle truncateAdd a class named
readMoreButtonto the button element.
You can remove your unnecessary classes and ids
var ReadMore = (function() {
function ReadMore(limit, readMoreContainerElementSelector) {
this.limit = limit;
this.readMoreContainerElementSelector = readMoreContainerElementSelector;
this.isFullTextShown = false;
this.initializeReadMore();
}
ReadMore.prototype.initializeReadMore = function() {
this.fullText = document.querySelector(this.readMoreContainerElementSelector + " .readMoreText").innerHTML;
this.truncatedText = this.fullText.substr(0, this.limit) + ' ...';
var that = this;
document.querySelector(this.readMoreContainerElementSelector + " .readMoreButton").addEventListener("click", function() {
that.performToogle();
});
this.performToogle();
};
ReadMore.prototype.performToogle = function() {
var textToDisplay = "";
var buttonText = "";
this.isFullTextShown ? (textToDisplay = this.fullText, buttonText = "Read Less" ): (textToDisplay = this.truncatedText, buttonText = "Read More");
this.isFullTextShown = !this.isFullTextShown;
document.querySelector(this.readMoreContainerElementSelector + " .readMoreText").innerHTML = textToDisplay;
document.querySelector(this.readMoreContainerElementSelector + " .readMoreButton").innerHTML = buttonText;
};
return ReadMore;
}());
var readmore = new ReadMore(100, "#readMoreContainer1");
var readmore = new ReadMore(50, "#readMoreContainer2");<div id="readMoreContainer1">
<section class="demo readMoreText" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn readMoreButton">Read More</button>
</div>
<hr/>
<div id="readMoreContainer2">
<section class="demo readMoreText" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn readMoreButton">Read More</button>
</div>add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
You don't need javascript for this. a simple css text-overflow: ellipsis; can do the trick.
It is a better/standard way to truncate the long text because it will truncate the text display at exact position which is depend on the font-size, width of parent container, etc... that is not possible simply with js. here is a demo:
var textHolder = document.querySelector('.demo');
var btn = document.querySelector('.btn');
function toggleText() {
textHolder.classList.toggle("truncate");
}
btn.addEventListener('click', toggleText);
toggleText(); //to truncate at first time.truncate {
text-overflow: ellipsis;
/*BOTH of the following are required for text-overflow (overflow: hidden; and white-space: nowrap;)*/
overflow: hidden;
white-space: nowrap;
}<section class="demo" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn">Read More</button>Note that cutting innerHTML can be dangerous as you may cut at improper position and corrupt the HTML code opening/closing tags...
2
a better answer indeed :)
– Towkir Ahmed
Nov 20 at 5:14
add a comment |
up vote
3
down vote
You don't need javascript for this. a simple css text-overflow: ellipsis; can do the trick.
It is a better/standard way to truncate the long text because it will truncate the text display at exact position which is depend on the font-size, width of parent container, etc... that is not possible simply with js. here is a demo:
var textHolder = document.querySelector('.demo');
var btn = document.querySelector('.btn');
function toggleText() {
textHolder.classList.toggle("truncate");
}
btn.addEventListener('click', toggleText);
toggleText(); //to truncate at first time.truncate {
text-overflow: ellipsis;
/*BOTH of the following are required for text-overflow (overflow: hidden; and white-space: nowrap;)*/
overflow: hidden;
white-space: nowrap;
}<section class="demo" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn">Read More</button>Note that cutting innerHTML can be dangerous as you may cut at improper position and corrupt the HTML code opening/closing tags...
2
a better answer indeed :)
– Towkir Ahmed
Nov 20 at 5:14
add a comment |
up vote
3
down vote
up vote
3
down vote
You don't need javascript for this. a simple css text-overflow: ellipsis; can do the trick.
It is a better/standard way to truncate the long text because it will truncate the text display at exact position which is depend on the font-size, width of parent container, etc... that is not possible simply with js. here is a demo:
var textHolder = document.querySelector('.demo');
var btn = document.querySelector('.btn');
function toggleText() {
textHolder.classList.toggle("truncate");
}
btn.addEventListener('click', toggleText);
toggleText(); //to truncate at first time.truncate {
text-overflow: ellipsis;
/*BOTH of the following are required for text-overflow (overflow: hidden; and white-space: nowrap;)*/
overflow: hidden;
white-space: nowrap;
}<section class="demo" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn">Read More</button>Note that cutting innerHTML can be dangerous as you may cut at improper position and corrupt the HTML code opening/closing tags...
You don't need javascript for this. a simple css text-overflow: ellipsis; can do the trick.
It is a better/standard way to truncate the long text because it will truncate the text display at exact position which is depend on the font-size, width of parent container, etc... that is not possible simply with js. here is a demo:
var textHolder = document.querySelector('.demo');
var btn = document.querySelector('.btn');
function toggleText() {
textHolder.classList.toggle("truncate");
}
btn.addEventListener('click', toggleText);
toggleText(); //to truncate at first time.truncate {
text-overflow: ellipsis;
/*BOTH of the following are required for text-overflow (overflow: hidden; and white-space: nowrap;)*/
overflow: hidden;
white-space: nowrap;
}<section class="demo" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn">Read More</button>Note that cutting innerHTML can be dangerous as you may cut at improper position and corrupt the HTML code opening/closing tags...
var textHolder = document.querySelector('.demo');
var btn = document.querySelector('.btn');
function toggleText() {
textHolder.classList.toggle("truncate");
}
btn.addEventListener('click', toggleText);
toggleText(); //to truncate at first time.truncate {
text-overflow: ellipsis;
/*BOTH of the following are required for text-overflow (overflow: hidden; and white-space: nowrap;)*/
overflow: hidden;
white-space: nowrap;
}<section class="demo" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn">Read More</button>var textHolder = document.querySelector('.demo');
var btn = document.querySelector('.btn');
function toggleText() {
textHolder.classList.toggle("truncate");
}
btn.addEventListener('click', toggleText);
toggleText(); //to truncate at first time.truncate {
text-overflow: ellipsis;
/*BOTH of the following are required for text-overflow (overflow: hidden; and white-space: nowrap;)*/
overflow: hidden;
white-space: nowrap;
}<section class="demo" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn">Read More</button>edited Nov 20 at 5:23
answered Nov 20 at 5:06
S.Serpooshan
3,93821637
3,93821637
2
a better answer indeed :)
– Towkir Ahmed
Nov 20 at 5:14
add a comment |
2
a better answer indeed :)
– Towkir Ahmed
Nov 20 at 5:14
2
2
a better answer indeed :)
– Towkir Ahmed
Nov 20 at 5:14
a better answer indeed :)
– Towkir Ahmed
Nov 20 at 5:14
add a comment |
up vote
1
down vote
First you need to store the full text to a variable, never change that variable, then store the truncated text into another variable and finally toggle between those to variable value to set the text to your target element.
Here is the sinppet:
var textHolder = document.querySelector('.demo');
var fullText = textHolder.innerHTML;
var btn = document.querySelector('.btn');
var textStatus = 'full'; // use this to check the status of text and toggle;
function Truancate(textHolder, limit) {
let txt = textHolder.innerHTML;
if (txt.length > limit) {
let newText = txt.substr(0, limit) + ' ...';
textHolder.innerHTML = newText;
textStatus = 'truncated';
}
}
Truancate(textHolder, textHolder.offsetWidth / 10);
function toggleText() {
// here i want to show full text...
// and also -> btn.innerHTML = 'Hide Text' | 'Show Text;
if (textStatus === 'truncated') {
textHolder.innerHTML = fullText;
textStatus = 'full';
} else {
Truancate(textHolder, textHolder.offsetWidth / 10);
}
}
btn.addEventListener('click', toggleText);<section class="demo" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn">Read More</button>add a comment |
up vote
1
down vote
First you need to store the full text to a variable, never change that variable, then store the truncated text into another variable and finally toggle between those to variable value to set the text to your target element.
Here is the sinppet:
var textHolder = document.querySelector('.demo');
var fullText = textHolder.innerHTML;
var btn = document.querySelector('.btn');
var textStatus = 'full'; // use this to check the status of text and toggle;
function Truancate(textHolder, limit) {
let txt = textHolder.innerHTML;
if (txt.length > limit) {
let newText = txt.substr(0, limit) + ' ...';
textHolder.innerHTML = newText;
textStatus = 'truncated';
}
}
Truancate(textHolder, textHolder.offsetWidth / 10);
function toggleText() {
// here i want to show full text...
// and also -> btn.innerHTML = 'Hide Text' | 'Show Text;
if (textStatus === 'truncated') {
textHolder.innerHTML = fullText;
textStatus = 'full';
} else {
Truancate(textHolder, textHolder.offsetWidth / 10);
}
}
btn.addEventListener('click', toggleText);<section class="demo" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn">Read More</button>add a comment |
up vote
1
down vote
up vote
1
down vote
First you need to store the full text to a variable, never change that variable, then store the truncated text into another variable and finally toggle between those to variable value to set the text to your target element.
Here is the sinppet:
var textHolder = document.querySelector('.demo');
var fullText = textHolder.innerHTML;
var btn = document.querySelector('.btn');
var textStatus = 'full'; // use this to check the status of text and toggle;
function Truancate(textHolder, limit) {
let txt = textHolder.innerHTML;
if (txt.length > limit) {
let newText = txt.substr(0, limit) + ' ...';
textHolder.innerHTML = newText;
textStatus = 'truncated';
}
}
Truancate(textHolder, textHolder.offsetWidth / 10);
function toggleText() {
// here i want to show full text...
// and also -> btn.innerHTML = 'Hide Text' | 'Show Text;
if (textStatus === 'truncated') {
textHolder.innerHTML = fullText;
textStatus = 'full';
} else {
Truancate(textHolder, textHolder.offsetWidth / 10);
}
}
btn.addEventListener('click', toggleText);<section class="demo" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn">Read More</button>First you need to store the full text to a variable, never change that variable, then store the truncated text into another variable and finally toggle between those to variable value to set the text to your target element.
Here is the sinppet:
var textHolder = document.querySelector('.demo');
var fullText = textHolder.innerHTML;
var btn = document.querySelector('.btn');
var textStatus = 'full'; // use this to check the status of text and toggle;
function Truancate(textHolder, limit) {
let txt = textHolder.innerHTML;
if (txt.length > limit) {
let newText = txt.substr(0, limit) + ' ...';
textHolder.innerHTML = newText;
textStatus = 'truncated';
}
}
Truancate(textHolder, textHolder.offsetWidth / 10);
function toggleText() {
// here i want to show full text...
// and also -> btn.innerHTML = 'Hide Text' | 'Show Text;
if (textStatus === 'truncated') {
textHolder.innerHTML = fullText;
textStatus = 'full';
} else {
Truancate(textHolder, textHolder.offsetWidth / 10);
}
}
btn.addEventListener('click', toggleText);<section class="demo" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn">Read More</button>var textHolder = document.querySelector('.demo');
var fullText = textHolder.innerHTML;
var btn = document.querySelector('.btn');
var textStatus = 'full'; // use this to check the status of text and toggle;
function Truancate(textHolder, limit) {
let txt = textHolder.innerHTML;
if (txt.length > limit) {
let newText = txt.substr(0, limit) + ' ...';
textHolder.innerHTML = newText;
textStatus = 'truncated';
}
}
Truancate(textHolder, textHolder.offsetWidth / 10);
function toggleText() {
// here i want to show full text...
// and also -> btn.innerHTML = 'Hide Text' | 'Show Text;
if (textStatus === 'truncated') {
textHolder.innerHTML = fullText;
textStatus = 'full';
} else {
Truancate(textHolder, textHolder.offsetWidth / 10);
}
}
btn.addEventListener('click', toggleText);<section class="demo" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn">Read More</button>var textHolder = document.querySelector('.demo');
var fullText = textHolder.innerHTML;
var btn = document.querySelector('.btn');
var textStatus = 'full'; // use this to check the status of text and toggle;
function Truancate(textHolder, limit) {
let txt = textHolder.innerHTML;
if (txt.length > limit) {
let newText = txt.substr(0, limit) + ' ...';
textHolder.innerHTML = newText;
textStatus = 'truncated';
}
}
Truancate(textHolder, textHolder.offsetWidth / 10);
function toggleText() {
// here i want to show full text...
// and also -> btn.innerHTML = 'Hide Text' | 'Show Text;
if (textStatus === 'truncated') {
textHolder.innerHTML = fullText;
textStatus = 'full';
} else {
Truancate(textHolder, textHolder.offsetWidth / 10);
}
}
btn.addEventListener('click', toggleText);<section class="demo" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn">Read More</button>answered Nov 20 at 5:08
Towkir Ahmed
920619
920619
add a comment |
add a comment |
up vote
0
down vote
Here is some more object oriented way and can be used anywhere in like situation
What you need to do is only follow following step
Create a wrapper container with an id let' say
readMoreContainerAdd a class named
readMoreTextto the section containing the text to toggle truncateAdd a class named
readMoreButtonto the button element.
You can remove your unnecessary classes and ids
var ReadMore = (function() {
function ReadMore(limit, readMoreContainerElementSelector) {
this.limit = limit;
this.readMoreContainerElementSelector = readMoreContainerElementSelector;
this.isFullTextShown = false;
this.initializeReadMore();
}
ReadMore.prototype.initializeReadMore = function() {
this.fullText = document.querySelector(this.readMoreContainerElementSelector + " .readMoreText").innerHTML;
this.truncatedText = this.fullText.substr(0, this.limit) + ' ...';
var that = this;
document.querySelector(this.readMoreContainerElementSelector + " .readMoreButton").addEventListener("click", function() {
that.performToogle();
});
this.performToogle();
};
ReadMore.prototype.performToogle = function() {
var textToDisplay = "";
var buttonText = "";
this.isFullTextShown ? (textToDisplay = this.fullText, buttonText = "Read Less" ): (textToDisplay = this.truncatedText, buttonText = "Read More");
this.isFullTextShown = !this.isFullTextShown;
document.querySelector(this.readMoreContainerElementSelector + " .readMoreText").innerHTML = textToDisplay;
document.querySelector(this.readMoreContainerElementSelector + " .readMoreButton").innerHTML = buttonText;
};
return ReadMore;
}());
var readmore = new ReadMore(100, "#readMoreContainer1");
var readmore = new ReadMore(50, "#readMoreContainer2");<div id="readMoreContainer1">
<section class="demo readMoreText" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn readMoreButton">Read More</button>
</div>
<hr/>
<div id="readMoreContainer2">
<section class="demo readMoreText" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn readMoreButton">Read More</button>
</div>add a comment |
up vote
0
down vote
Here is some more object oriented way and can be used anywhere in like situation
What you need to do is only follow following step
Create a wrapper container with an id let' say
readMoreContainerAdd a class named
readMoreTextto the section containing the text to toggle truncateAdd a class named
readMoreButtonto the button element.
You can remove your unnecessary classes and ids
var ReadMore = (function() {
function ReadMore(limit, readMoreContainerElementSelector) {
this.limit = limit;
this.readMoreContainerElementSelector = readMoreContainerElementSelector;
this.isFullTextShown = false;
this.initializeReadMore();
}
ReadMore.prototype.initializeReadMore = function() {
this.fullText = document.querySelector(this.readMoreContainerElementSelector + " .readMoreText").innerHTML;
this.truncatedText = this.fullText.substr(0, this.limit) + ' ...';
var that = this;
document.querySelector(this.readMoreContainerElementSelector + " .readMoreButton").addEventListener("click", function() {
that.performToogle();
});
this.performToogle();
};
ReadMore.prototype.performToogle = function() {
var textToDisplay = "";
var buttonText = "";
this.isFullTextShown ? (textToDisplay = this.fullText, buttonText = "Read Less" ): (textToDisplay = this.truncatedText, buttonText = "Read More");
this.isFullTextShown = !this.isFullTextShown;
document.querySelector(this.readMoreContainerElementSelector + " .readMoreText").innerHTML = textToDisplay;
document.querySelector(this.readMoreContainerElementSelector + " .readMoreButton").innerHTML = buttonText;
};
return ReadMore;
}());
var readmore = new ReadMore(100, "#readMoreContainer1");
var readmore = new ReadMore(50, "#readMoreContainer2");<div id="readMoreContainer1">
<section class="demo readMoreText" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn readMoreButton">Read More</button>
</div>
<hr/>
<div id="readMoreContainer2">
<section class="demo readMoreText" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn readMoreButton">Read More</button>
</div>add a comment |
up vote
0
down vote
up vote
0
down vote
Here is some more object oriented way and can be used anywhere in like situation
What you need to do is only follow following step
Create a wrapper container with an id let' say
readMoreContainerAdd a class named
readMoreTextto the section containing the text to toggle truncateAdd a class named
readMoreButtonto the button element.
You can remove your unnecessary classes and ids
var ReadMore = (function() {
function ReadMore(limit, readMoreContainerElementSelector) {
this.limit = limit;
this.readMoreContainerElementSelector = readMoreContainerElementSelector;
this.isFullTextShown = false;
this.initializeReadMore();
}
ReadMore.prototype.initializeReadMore = function() {
this.fullText = document.querySelector(this.readMoreContainerElementSelector + " .readMoreText").innerHTML;
this.truncatedText = this.fullText.substr(0, this.limit) + ' ...';
var that = this;
document.querySelector(this.readMoreContainerElementSelector + " .readMoreButton").addEventListener("click", function() {
that.performToogle();
});
this.performToogle();
};
ReadMore.prototype.performToogle = function() {
var textToDisplay = "";
var buttonText = "";
this.isFullTextShown ? (textToDisplay = this.fullText, buttonText = "Read Less" ): (textToDisplay = this.truncatedText, buttonText = "Read More");
this.isFullTextShown = !this.isFullTextShown;
document.querySelector(this.readMoreContainerElementSelector + " .readMoreText").innerHTML = textToDisplay;
document.querySelector(this.readMoreContainerElementSelector + " .readMoreButton").innerHTML = buttonText;
};
return ReadMore;
}());
var readmore = new ReadMore(100, "#readMoreContainer1");
var readmore = new ReadMore(50, "#readMoreContainer2");<div id="readMoreContainer1">
<section class="demo readMoreText" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn readMoreButton">Read More</button>
</div>
<hr/>
<div id="readMoreContainer2">
<section class="demo readMoreText" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn readMoreButton">Read More</button>
</div>Here is some more object oriented way and can be used anywhere in like situation
What you need to do is only follow following step
Create a wrapper container with an id let' say
readMoreContainerAdd a class named
readMoreTextto the section containing the text to toggle truncateAdd a class named
readMoreButtonto the button element.
You can remove your unnecessary classes and ids
var ReadMore = (function() {
function ReadMore(limit, readMoreContainerElementSelector) {
this.limit = limit;
this.readMoreContainerElementSelector = readMoreContainerElementSelector;
this.isFullTextShown = false;
this.initializeReadMore();
}
ReadMore.prototype.initializeReadMore = function() {
this.fullText = document.querySelector(this.readMoreContainerElementSelector + " .readMoreText").innerHTML;
this.truncatedText = this.fullText.substr(0, this.limit) + ' ...';
var that = this;
document.querySelector(this.readMoreContainerElementSelector + " .readMoreButton").addEventListener("click", function() {
that.performToogle();
});
this.performToogle();
};
ReadMore.prototype.performToogle = function() {
var textToDisplay = "";
var buttonText = "";
this.isFullTextShown ? (textToDisplay = this.fullText, buttonText = "Read Less" ): (textToDisplay = this.truncatedText, buttonText = "Read More");
this.isFullTextShown = !this.isFullTextShown;
document.querySelector(this.readMoreContainerElementSelector + " .readMoreText").innerHTML = textToDisplay;
document.querySelector(this.readMoreContainerElementSelector + " .readMoreButton").innerHTML = buttonText;
};
return ReadMore;
}());
var readmore = new ReadMore(100, "#readMoreContainer1");
var readmore = new ReadMore(50, "#readMoreContainer2");<div id="readMoreContainer1">
<section class="demo readMoreText" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn readMoreButton">Read More</button>
</div>
<hr/>
<div id="readMoreContainer2">
<section class="demo readMoreText" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn readMoreButton">Read More</button>
</div>var ReadMore = (function() {
function ReadMore(limit, readMoreContainerElementSelector) {
this.limit = limit;
this.readMoreContainerElementSelector = readMoreContainerElementSelector;
this.isFullTextShown = false;
this.initializeReadMore();
}
ReadMore.prototype.initializeReadMore = function() {
this.fullText = document.querySelector(this.readMoreContainerElementSelector + " .readMoreText").innerHTML;
this.truncatedText = this.fullText.substr(0, this.limit) + ' ...';
var that = this;
document.querySelector(this.readMoreContainerElementSelector + " .readMoreButton").addEventListener("click", function() {
that.performToogle();
});
this.performToogle();
};
ReadMore.prototype.performToogle = function() {
var textToDisplay = "";
var buttonText = "";
this.isFullTextShown ? (textToDisplay = this.fullText, buttonText = "Read Less" ): (textToDisplay = this.truncatedText, buttonText = "Read More");
this.isFullTextShown = !this.isFullTextShown;
document.querySelector(this.readMoreContainerElementSelector + " .readMoreText").innerHTML = textToDisplay;
document.querySelector(this.readMoreContainerElementSelector + " .readMoreButton").innerHTML = buttonText;
};
return ReadMore;
}());
var readmore = new ReadMore(100, "#readMoreContainer1");
var readmore = new ReadMore(50, "#readMoreContainer2");<div id="readMoreContainer1">
<section class="demo readMoreText" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn readMoreButton">Read More</button>
</div>
<hr/>
<div id="readMoreContainer2">
<section class="demo readMoreText" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn readMoreButton">Read More</button>
</div>var ReadMore = (function() {
function ReadMore(limit, readMoreContainerElementSelector) {
this.limit = limit;
this.readMoreContainerElementSelector = readMoreContainerElementSelector;
this.isFullTextShown = false;
this.initializeReadMore();
}
ReadMore.prototype.initializeReadMore = function() {
this.fullText = document.querySelector(this.readMoreContainerElementSelector + " .readMoreText").innerHTML;
this.truncatedText = this.fullText.substr(0, this.limit) + ' ...';
var that = this;
document.querySelector(this.readMoreContainerElementSelector + " .readMoreButton").addEventListener("click", function() {
that.performToogle();
});
this.performToogle();
};
ReadMore.prototype.performToogle = function() {
var textToDisplay = "";
var buttonText = "";
this.isFullTextShown ? (textToDisplay = this.fullText, buttonText = "Read Less" ): (textToDisplay = this.truncatedText, buttonText = "Read More");
this.isFullTextShown = !this.isFullTextShown;
document.querySelector(this.readMoreContainerElementSelector + " .readMoreText").innerHTML = textToDisplay;
document.querySelector(this.readMoreContainerElementSelector + " .readMoreButton").innerHTML = buttonText;
};
return ReadMore;
}());
var readmore = new ReadMore(100, "#readMoreContainer1");
var readmore = new ReadMore(50, "#readMoreContainer2");<div id="readMoreContainer1">
<section class="demo readMoreText" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn readMoreButton">Read More</button>
</div>
<hr/>
<div id="readMoreContainer2">
<section class="demo readMoreText" id="demo">
Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn readMoreButton">Read More</button>
</div>answered Nov 20 at 5:54
Samuel J Mathew
3,40412228
3,40412228
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386455%2fhow-to-toggle-truncate-text-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown