Simple configuration, just add configuration json to html
<div class="element" data-config='{ "type": "text", "limit": 120, "more": "→ show more", "less": "← less" }'>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Esse, quos qui. Recusandae minima esse qui dicta iusto quod quam exercitationem.
</div>
And run the function
new ShowMore('.element');
You can also use the callback function, see last example
new ShowMore('.element', {
onMoreLess: (type, object) => {
console.log(type, object);
}
});
You can add your own regular expression
new ShowMore('.element', {
regex: {
image: {
match: /<img([\w\W]+?)[/]?>/g,
replace: ''
}
}
});
If you have one type of items that you want to shorten, you can add global configuration, you don't need to add data-config to each item. Below is an example:
new ShowMore('.element', {
config: {
type: "text",
limit: 120,
more: "→ read more",
less: "← read less"
}
});
<div class="element" data-config='{ "limit": 20 }'>
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Quo, deleniti?
</div>
new ShowMore('.element', {
config: {
type: "text",
limit: 200,
more: "→ read more",
less: "← read less"
}
});
{
"type": "text",
"limit": 120, // show max chars
"more": "→ show more",
"less": "← less"
}
{
"type": "text",
"limit": 120,
"after": 50, // prevent cutting after 120 characters
"more": "→ show more",
"less": "← less"
}
{
"type": "text",
"limit": 120,
"after": 50,
"more": "→ show more",
"less": "← less"
}
{
"type": "text",
"limit": 90,
"element": "div", // adds a 'div' on a new line
"after": 50,
"more": "↓ show more",
"less": "↑ less"
}
{
"type": "list",
"limit": 5,
"element": "li",
"more": "↓ show more",
"less": "↑ less",
"number": true // adds the number of items to the button
}
{
"type": "list",
"limit": 3,
"element": "li",
"more": "→ show more",
"less": "← less",
"number": true
}
{
"type": "list",
"limit": 5,
"element": "div",
"more": "↓ show more",
"less": "↑ less"
}
| ONE | TWO | THRE |
|---|---|---|
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |
| 10 | 11 | 12 |
| 13 | 14 | 15 |
| = 35 | = 40 | = 45 |
{
"type": "table",
"limit": 4,
"more": "↓ show more",
"less": "↑ less",
"number": true
}
{
"type": "list",
"limit": 5,
"more": "→ show more" // only the 'show more' button
}
{
"type": "text",
// cropping the text after 100 characters
// and adding an ellipse
"limit": 100,
// parameter needed when the global configuration
// has a less/more button
"nobutton": true
}
new ShowMore('.element', {
onMoreLess: (type, object) => {
const index = object.index;
const h2 = object.element.parentElement.firstElementChild;
if (index == 11) {
if (type === 'expand') {
h2.setAttribute('style', 'color: #eb00ff;');
console.log(object);
} else {
h2.removeAttribute('style');
console.log(object);
}
}
}
});
<strong></strong> and
<b></b> should be placed outside the visible
shortened element.
| one | two | three |
|---|---|---|
| a | b | c |
| d | e | f |
Fuga, libero eaque consequuntur ipsa esse omnis, ad eius laboriosam
reprehenderit iste quaerat vitae quis corrupti saepe veniam, ullam
placeat voluptatum sint dolore sunt quo. Voluptate fugit quo
architecto laboriosam
ipsam pariatur delectus iusto consectetur provident odio amet
tempora veniam velit at deleniti sint soluta accusamus, praesentium
necessitatibus maxime.
{
"type": "text",
"limit": 100,
"more": "→ read more",
"less": "← read less"
}
new ShowMore('.element', {
config: {
type: "text",
limit: 20,
more: "→ read more",
less: "← read less"
}
});