1612260600
JavaScript library that truncates text, list or table by chars, elements or row and shows/hides text blocks, elements or table row with Show More and Show Less.
See the demo - example
git clone
cd show-more
yarn
// or
npm i
Watch the app, just call:
yarn dev
// or
npm run dev
Build app:
yarn prod
// or
npm run prod
<link rel="stylesheet" href="style.css">
<script src="showMore.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/tomik23/show-more@master/docs/style.css">
<script src="https://cdn.jsdelivr.net/gh/tomik23/show-more@master/docs/showMore.min.js"></script>
<div class="element" data-config='{ "type": "text", "limit": 120, "more": "→ show more", "less": "← less" }'>
Lorem ipsum, dolor ...
...
</div>
<ul class="element" data-config='{ "type": "list", "limit": 5, "element": "li", "more": "↓ show more", "less": "↑ less", "number": true }'>
<li>item 1</li>
<li>item 2</li>
...
</ul>
<table class="element" data-config='{ "type": "table", "limit": 4, "more": "↓ show more", "less": "↑ less", "number": true }'>
...
</table>
show more
button → live example:<div class="element links-style" data-config='{ "type": "list", "limit": 5, "more": "→ show more" }'>
<a href="#">Administracja biur,</a>
</div>
<div class="element" data-config='{ "type": "text", "limit": 100, "ellipsis": " ..." }'>
It is a long established fact that a reader will be distracted by the readable content of a page when looking
at its layout.
</div>
<ul class="element display-inline" data-config='{ "type": "list", "limit": 3, "element": "li", "more": "→ show more", "less": "← less", "number": true }'>
<li>Usługi murarskie i tynkarskie,</li>
...
</ul>
element | description |
---|---|
your-class |
name of the class after which we want to add support for showing/hiding text, list or table |
data-config |
embedding JSON in the html, the entire configuration of a particular element |
type |
we have three type after which it will be hidden [text, list or table] |
limit |
text after how many characters to hide the text and insert show more/less |
list or table after how many elements/rows hide the rest and insert show more/less |
|
after |
this parameter checks how much text is after the trimmed text the limit parameter, if the text is less than the after parameter does not add a more/less button`^. |
element |
on the parameter we will create an html element and put in the text show more/less |
more/less |
is the text and chars that appears after the text, list or table e.g. > show more and < show less |
number |
number of hidden items to show more/less e.g. -> show more 3 , only works for list and table |
ellipsis |
show only the ellipsis |
onAction |
callback function |
^ Let’s say we have 20 records with text and we determine that the text is to be trimmed after 100 characters in each record, it may happen that in several records the text is very short and has 110 characters, so
show more/less
will appear after 100 characters and after clicking an additional 10 characters, it will look funny. To prevent this, we add the"after": 50
parameter, which means that the hidden text must be at least 50 characters. Otherwise,show more/less
will not appear. The sameafter
can be applied to lists, elements and table records
Number of records counted in the table
tr
based on alltr
ofthead
,tbody
andtfoot
document.addEventListener('DOMContentLoaded', function() {
// text, table, list, elelemnts
new ShowMore('.element', {
onAction: (type, object) => {
// type = less/more and full object
console.log(type, object);
}
});
});
Author: tomik23
Demo: https://tomik23.github.io/show-more/
Source Code: https://github.com/tomik23/show-more
#javascript
1650870267
In the previous chapters you've learnt how to select individual elements on a web page. But there are many occasions where you need to access a child, parent or ancestor element. See the JavaScript DOM nodes chapter to understand the logical relationships between the nodes in a DOM tree.
DOM node provides several properties and methods that allow you to navigate or traverse through the tree structure of the DOM and make changes very easily. In the following section we will learn how to navigate up, down, and sideways in the DOM tree using JavaScript.
You can use the firstChild
and lastChild
properties of the DOM node to access the first and last direct child node of a node, respectively. If the node doesn't have any child element, it returns null
.
<div id="main">
<h1 id="title">My Heading</h1>
<p id="hint"><span>This is some text.</span></p>
</div>
<script>
var main = document.getElementById("main");
console.log(main.firstChild.nodeName); // Prints: #text
var hint = document.getElementById("hint");
console.log(hint.firstChild.nodeName); // Prints: SPAN
</script>
Note: The
nodeName
is a read-only property that returns the name of the current node as a string. For example, it returns the tag name for element node,#text
for text node,#comment
for comment node,#document
for document node, and so on.
If you notice the above example, the nodeName
of the first-child node of the main DIV element returns #text instead of H1. Because, whitespace such as spaces, tabs, newlines, etc. are valid characters and they form #text nodes and become a part of the DOM tree. Therefore, since the <div>
tag contains a newline before the <h1>
tag, so it will create a #text node.
To avoid the issue with firstChild
and lastChild
returning #text or #comment nodes, you could alternatively use the firstElementChild
and lastElementChild
properties to return only the first and last element node, respectively. But, it will not work in IE 9 and earlier.
<div id="main">
<h1 id="title">My Heading</h1>
<p id="hint"><span>This is some text.</span></p>
</div>
<script>
var main = document.getElementById("main");
alert(main.firstElementChild.nodeName); // Outputs: H1
main.firstElementChild.style.color = "red";
var hint = document.getElementById("hint");
alert(hint.firstElementChild.nodeName); // Outputs: SPAN
hint.firstElementChild.style.color = "blue";
</script>
Similarly, you can use the childNodes
property to access all child nodes of a given element, where the first child node is assigned index 0. Here's an example:
<div id="main">
<h1 id="title">My Heading</h1>
<p id="hint"><span>This is some text.</span></p>
</div>
<script>
var main = document.getElementById("main");
// First check that the element has child nodes
if(main.hasChildNodes()) {
var nodes = main.childNodes;
// Loop through node list and display node name
for(var i = 0; i < nodes.length; i++) {
alert(nodes[i].nodeName);
}
}
</script>
The childNodes
returns all child nodes, including non-element nodes like text and comment nodes. To get a collection of only elements, use children
property instead.
<div id="main">
<h1 id="title">My Heading</h1>
<p id="hint"><span>This is some text.</span></p>
</div>
<script>
var main = document.getElementById("main");
// First check that the element has child nodes
if(main.hasChildNodes()) {
var nodes = main.children;
// Loop through node list and display node name
for(var i = 0; i < nodes.length; i++) {
alert(nodes[i].nodeName);
}
}
</script>
1604191980
JavaScript library that truncates text, list or table by chars, elements or row and shows/hides text blocks, elements or table row with Show More and Show Less.
See the demo - example
git clone
cd show-more
yarn
// or
npm i
Watch the app, just call:
yarn dev
// or
npm run dev
Build app:
yarn prod
// or
npm run prod
<link rel="stylesheet" href="style.css">
<script src="showMore.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/tomik23/show-more@master/docs/style.css">
<script src="https://cdn.jsdelivr.net/gh/tomik23/show-more@master/docs/showMore.min.js"></script>
<div class="element" data-config="{ 'type': 'text', 'limit': 120, 'more': '→ show more', 'less': '← less' }">
Lorem ipsum, dolor ...
...
</div>
<ul class="element" data-config="{ 'type': 'list', 'limit': 5, 'element': 'li', 'more': '↓ show more', 'less': '↑ less', 'number': true }">
<li>item 1</li>
<li>item 2</li>
...
</ul>
<table class="element" data-config="{ 'type': 'table', 'limit': 4, 'more': '↓ show more', 'less': '↑ less', 'number': true }">
...
</table>
show more
button → live example:<div class="element links-style" data-config="{ 'type': 'list', 'limit': 5, 'more': '→ show more' }">
<a href="#">Administracja biur,</a>
</div>
<ul class="element display-inline" data-config="{ 'type': 'list', 'limit': 3, 'element': 'li', 'more': '→ show more', 'less': '← less', 'number': true }">
<li>Usługi murarskie i tynkarskie,</li>
...
</ul>
element | description |
---|---|
your-class |
name of the class after which we want to add support for showing/hiding text, list or table |
data-config |
embedding JSON in the html, the entire configuration of a particular element |
type |
we have three type after which it will be hidden [text, list or table] |
limit |
text after how many characters to hide the text and insert show more/less |
list or table after how many elements/rows hide the rest and insert show more/less |
|
after |
this parameter allows you to set how much text/elements/rows |
should be after the show more/less button ^. |
|
element |
on the parameter we will create an html element and put in the text show more/less |
more/less |
is the text and chars that appears after the text, list or table e.g. > show more and < show less |
number |
number of hidden items to show more/less e.g. -> show more 3 , only works for list and table |
^ Let’s say we have 20 records with text and we determine that the text is to be trimmed after 100 characters in each record, it may happen that in several records the text is very short and has 110 characters, so
show more/less
will appear after 100 characters and after clicking an additional 10 characters, it will look funny. To prevent this, we add the"after": 50
parameter, which means that the hidden text must be at least 50 characters. Otherwise,show more/less
will not appear. The sameafter
can be applied to lists, elements and table records
Number of records counted in the table
tr
based on alltr
ofthead
,tbody
andtfoot
document.addEventListener('DOMContentLoaded', function() {
// text, table, list, elelemnts
new ShowMore('.element');
});
});
Author: tomik23
Demo: https://tomik23.github.io/show-more/
Source Code: https://github.com/tomik23/show-more
#javascript
1595577240
JavaScript library that truncates text, list or table by chars, elements or row and shows/hides text blocks, elements or table row with Show More and Show Less.
git clone
cd show-more
yarn
# or
npm i
Watch the app, just call:
yarn dev
# or
npm run dev
Build app:
yarn prod
# or
npm run prod
<link rel="stylesheet" href="style.css">
<script src="showMore.min.js"></script>
<div class="example-text" data-type="text" data-number="80" data-after="30">
Lorem ipsum, dolor ...
...
</div>
new ShowMore('example-text', {
more: ' → show more',
less: ' ← less'
});
<ul class="example-list" data-type="list" data-number="5" data-after="3">
<li>Import item 1</li>
<li>Import item 2</li>
...
</ul>
new ShowMore('example-list', {
type: 'li',
more: ' → show more',
less: ' ← less'
});
<div class="example-list-b" data-type="list" data-number="5" data-after="3">
<a href="#">item 1</a>
<a href="#">item 2</a>
...
</div>
new ShowMore('example-list-b', {
more: ' → show more',
less: ' ← less'
});
<table class="example-table" data-type="table" data-number="2" data-after="3">
...
</table>
new ShowMore('example-table', {
more: ' → show more',
less: ' ← less'
});
element | description |
---|---|
data-type |
we have three type after which it will be hidden [text, list or table] |
data-number |
text after how many characters to hide the text and insert show more/less |
list or table after how many elements/rows hide the rest and insert show more/less |
|
data-after |
this parameter allows you to set how much text/elements/rows |
should be after the show more/less button ^. |
^ Let’s say we have 20 records with text and we determine that the text is to be trimmed after 100 characters in each record, it may happen that in several records the text is very short and has 110 characters, so show more/less
will appear after 100 characters and after clicking an additional 10 characters, it will look funny. To prevent this, we add the data-after="50"
parameter, which means that the hidden text must be at least 50 characters. Otherwise, show more/less
will not appear. The same data-after
can be applied to lists, elements and table records
Number of records counted in the table tr
based on all tr
of thead
, tbody
and tfoot
document.addEventListener('DOMContentLoaded', function() {
// text, table, list, elelemnts
new ShowMore('your-class', {
type: 'span', // [div, li, a, ...] parameter not required
more: ' → show more', // text before expanding
less: ' ← less' // expanded text
});
});
element | description |
---|---|
your-class |
name of the class after which we want to add support for showing/hiding text, list or table |
type |
on the parameter we will create an html element and put in the text show more/less |
more/less |
is the text and chars that appears after the text, list or table e.g. > show more and < show less |
Author: tomik23
Live Demo: https://tomik23.github.io/show-more/
GitHub: https://github.com/tomik23/show-more
#javascript #programming
1612260600
JavaScript library that truncates text, list or table by chars, elements or row and shows/hides text blocks, elements or table row with Show More and Show Less.
See the demo - example
git clone
cd show-more
yarn
// or
npm i
Watch the app, just call:
yarn dev
// or
npm run dev
Build app:
yarn prod
// or
npm run prod
<link rel="stylesheet" href="style.css">
<script src="showMore.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/tomik23/show-more@master/docs/style.css">
<script src="https://cdn.jsdelivr.net/gh/tomik23/show-more@master/docs/showMore.min.js"></script>
<div class="element" data-config='{ "type": "text", "limit": 120, "more": "→ show more", "less": "← less" }'>
Lorem ipsum, dolor ...
...
</div>
<ul class="element" data-config='{ "type": "list", "limit": 5, "element": "li", "more": "↓ show more", "less": "↑ less", "number": true }'>
<li>item 1</li>
<li>item 2</li>
...
</ul>
<table class="element" data-config='{ "type": "table", "limit": 4, "more": "↓ show more", "less": "↑ less", "number": true }'>
...
</table>
show more
button → live example:<div class="element links-style" data-config='{ "type": "list", "limit": 5, "more": "→ show more" }'>
<a href="#">Administracja biur,</a>
</div>
<div class="element" data-config='{ "type": "text", "limit": 100, "ellipsis": " ..." }'>
It is a long established fact that a reader will be distracted by the readable content of a page when looking
at its layout.
</div>
<ul class="element display-inline" data-config='{ "type": "list", "limit": 3, "element": "li", "more": "→ show more", "less": "← less", "number": true }'>
<li>Usługi murarskie i tynkarskie,</li>
...
</ul>
element | description |
---|---|
your-class |
name of the class after which we want to add support for showing/hiding text, list or table |
data-config |
embedding JSON in the html, the entire configuration of a particular element |
type |
we have three type after which it will be hidden [text, list or table] |
limit |
text after how many characters to hide the text and insert show more/less |
list or table after how many elements/rows hide the rest and insert show more/less |
|
after |
this parameter checks how much text is after the trimmed text the limit parameter, if the text is less than the after parameter does not add a more/less button`^. |
element |
on the parameter we will create an html element and put in the text show more/less |
more/less |
is the text and chars that appears after the text, list or table e.g. > show more and < show less |
number |
number of hidden items to show more/less e.g. -> show more 3 , only works for list and table |
ellipsis |
show only the ellipsis |
onAction |
callback function |
^ Let’s say we have 20 records with text and we determine that the text is to be trimmed after 100 characters in each record, it may happen that in several records the text is very short and has 110 characters, so
show more/less
will appear after 100 characters and after clicking an additional 10 characters, it will look funny. To prevent this, we add the"after": 50
parameter, which means that the hidden text must be at least 50 characters. Otherwise,show more/less
will not appear. The sameafter
can be applied to lists, elements and table records
Number of records counted in the table
tr
based on alltr
ofthead
,tbody
andtfoot
document.addEventListener('DOMContentLoaded', function() {
// text, table, list, elelemnts
new ShowMore('.element', {
onAction: (type, object) => {
// type = less/more and full object
console.log(type, object);
}
});
});
Author: tomik23
Demo: https://tomik23.github.io/show-more/
Source Code: https://github.com/tomik23/show-more
#javascript
1605177504
In this video, I will be showing you how to turn text into speech in Node.js
#javascript #text to speech #javascript api #text to speech app #node.js text to speech #javascript text to speech