Reputation: 894
I want to make the extra text three dots (...) ellipsis and when i click the dots the text should expand and contract. But with the code used the text is only contracted but not expanding on click of dots
.overflow{
display:inline-block;
width:180px;
white-space: nowrap;
overflow:hidden !important;
text-overflow: ellipsis;
}
HTML Code
<div class="overflow" innerHTML="{{ post.review_Text | highlighter : reviewName}}"></div>
Upvotes: 4
Views: 24326
Reputation: 790
If you are using Bootstrap, you can apply the text-truncate
class to add an ellipsis to any text, like so:
<span id="ellipsis-ex" class="d-inline-block text-truncate" style="max-width: 150px;">
Praeterea iter est quasdam res quas ex communi.
</span>
Else, you can define the appropriate class to generate the ellipsis as you've mentioned in the question:
.text-truncate {
display: inline-block;
max-width: 150px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.text-truncate {
display: inline-block;
max-width: 150px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
<span class="text-truncate" style="max-width: 150px;">
Praeterea iter est quasdam res quas ex communi.
</span>
To toggle the class using pure JS, use
var element = document.querySelector("ellipsis-ex");
element.classList.toggle("text-truncate");
You can also use the classList.add("#ellipsis-ex")
and classList.remove("ellipsis-ex")
functions to specifically add or remove the class
Reading your question, it seems like you're working with Angular therefore you can use the built-in class
directive to toggle the class in the template itself.
<!-- toggleEllipses is a boolean indicating ellipsis status -->
<span id="ellipsis-ex" [class.text-truncate]="toggleEllipses" style="max-width: 150px;">
Praeterea iter est quasdam res quas ex communi.
</span>
var element = document.querySelector("#ellipsis-ex");
function toggleEllipsis() {
element.classList.toggle("text-truncate");
}
.text-truncate {
display: inline-block;
max-width: 150px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
<span id="ellipsis-ex" class="text-truncate" style="max-width: 150px;" onclick="toggleEllipsis()">
Praeterea iter est quasdam res quas ex communi.
</span>
Upvotes: 10
Reputation: 894
I got the answer i used :
<div [class.overflow]="isExpand" innerHTML="{{ post.review_Text | highlighter :
reviewName}}" (click)="isExpandToggle()"></div>
used the expand variable to toggle
isExpand:boolean=true;
isExpandToggle(){
this.isExpand=!this.isExpand;
}
overFlow css class
.overflow{
display:inline-block;
width:380px;
white-space: nowrap;
overflow:hidden !important;
text-overflow: ellipsis;
}
Upvotes: 1