-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TO - Additional Info Read More JS #457
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -310,4 +310,45 @@ if ( window.location.hash ) { | |
lsx_to.build_slider_lightbox(); | ||
} ); | ||
|
||
} )( jQuery, window, document ); | ||
document.addEventListener('DOMContentLoaded', function () { | ||
const paragraphs = document.querySelectorAll('.additional-info .wp-block-group.content p'); | ||
|
||
paragraphs.forEach(function (p) { | ||
const text = p.innerText.trim(); | ||
|
||
if (text.split(' ').length > 30) { // Check if paragraph exceeds 30 words | ||
const fullText = p.innerText.trim(); | ||
const truncatedText = fullText.split(' ').slice(0, 30).join(' ') + '...'; | ||
p.innerHTML = `<span class="truncated-text">${truncatedText}</span><span class="full-text" style="display: none;">${fullText}</span>`; | ||
|
||
// Create Read More button | ||
const readMoreBtn = document.createElement('span'); | ||
readMoreBtn.textContent = ' Read More'; | ||
readMoreBtn.classList.add('read-more-btn'); | ||
p.appendChild(readMoreBtn); | ||
|
||
// Create Read Less button | ||
const readLessBtn = document.createElement('span'); | ||
readLessBtn.textContent = ' Read Less'; | ||
readLessBtn.classList.add('read-less-btn'); | ||
p.appendChild(readLessBtn); | ||
|
||
// Add functionality to toggle text | ||
readMoreBtn.addEventListener('click', function () { | ||
p.querySelector('.truncated-text').style.display = 'none'; | ||
p.querySelector('.full-text').style.display = 'inline'; | ||
readMoreBtn.style.display = 'none'; | ||
readLessBtn.style.display = 'inline'; | ||
}); | ||
|
||
readLessBtn.addEventListener('click', function () { | ||
p.querySelector('.truncated-text').style.display = 'inline'; | ||
p.querySelector('.full-text').style.display = 'none'; | ||
readMoreBtn.style.display = 'inline'; | ||
readLessBtn.style.display = 'none'; | ||
}); | ||
} | ||
Comment on lines
+318
to
+352
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic for creating "Read More" and "Read Less" buttons is directly manipulating the DOM inside a loop. This could lead to performance issues if there are many paragraphs on the page. It would be more efficient to use event delegation, where you attach an event listener to a parent element and handle events from its child elements based on the event's target. Here's how you can refactor this code: - paragraphs.forEach(function (p) {
- const text = p.innerText.trim();
- if (text.split(' ').length > 30) { // Check if paragraph exceeds 30 words
- const fullText = p.innerText.trim();
- const truncatedText = fullText.split(' ').slice(0, 30).join(' ') + '...';
- p.innerHTML = `<span class="truncated-text">${truncatedText}</span><span class="full-text" style="display: none;">${fullText}</span>`;
-
- // Create Read More button
- const readMoreBtn = document.createElement('span');
- readMoreBtn.textContent = ' Read More';
- readMoreBtn.classList.add('read-more-btn');
- p.appendChild(readMoreBtn);
-
- // Create Read Less button
- const readLessBtn = document.createElement('span');
- readLessBtn.textContent = ' Read Less';
- readLessBtn.classList.add('read-less-btn');
- p.appendChild(readLessBtn);
-
- // Add functionality to toggle text
- readMoreBtn.addEventListener('click', function () {
- p.querySelector('.truncated-text').style.display = 'none';
- p.querySelector('.full-text').style.display = 'inline';
- readMoreBtn.style.display = 'none';
- readLessBtn.style.display = 'inline';
- });
-
- readLessBtn.addEventListener('click', function () {
- p.querySelector('.truncated-text').style.display = 'inline';
- p.querySelector('.full-text').style.display = 'none';
- readMoreBtn.style.display = 'inline';
- readLessBtn.style.display = 'none';
- });
- }
-});
+ document.querySelector('.additional-info .wp-block-group.content').addEventListener('click', function (event) {
+ if (event.target.matches('.read-more-btn')) {
+ const p = event.target.parentNode;
+ p.querySelector('.truncated-text').style.display = 'none';
+ p.querySelector('.full-text').style.display = 'inline';
+ p.querySelector('.read-more-btn').style.display = 'none';
+ p.querySelector('.read-less-btn').style.display = 'inline';
+ } else if (event.target.matches('.read-less-btn')) {
+ const p = event.target.parentNode;
+ p.querySelector('.truncated-text').style.display = 'inline';
+ p.querySelector('.full-text').style.display = 'none';
+ p.querySelector('.read-more-btn').style.display = 'inline';
+ p.querySelector('.read-less-btn').style.display = 'none';
+ }
+ }); |
||
}); | ||
}); | ||
Comment on lines
+315
to
+354
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This new code block adds a "Read More" / "Read Less" functionality to paragraphs that exceed 30 words. It seems to be well implemented, creating two buttons and toggling visibility between the truncated and full text versions of the paragraph. However, there are a few points to consider:
Here's a revised version of the code considering these points: document.addEventListener('DOMContentLoaded', function () {
const paragraphs = document.querySelectorAll('.additional-info .wp-block-group.content p');
const fragment = document.createDocumentFragment();
paragraphs.forEach(function (p) {
const text = p.innerText.trim();
if (text.split(' ').length > 30) { // Check if paragraph exceeds 30 words
const fullText = p.innerText.trim();
const truncatedText = fullText.split(' ').slice(0, 30).join(' ') + '...';
p.textContent = '';
const truncatedSpan = document.createElement('span');
truncatedSpan.className = 'truncated-text';
truncatedSpan.textContent = truncatedText;
const fullTextSpan = document.createElement('span');
fullTextSpan.className = 'full-text';
fullTextSpan.style.display = 'none';
fullTextSpan.textContent = fullText;
const readMoreBtn = document.createElement('button');
readMoreBtn.textContent = ' Read More';
readMoreBtn.classList.add('read-more-btn');
const readLessBtn = document.createElement('button');
readLessBtn.textContent = ' Read Less';
readLessBtn.classList.add('read-less-btn');
readLessBtn.style.display = 'none';
readMoreBtn.addEventListener('click', function () {
truncatedSpan.style.display = 'none';
fullTextSpan.style.display = 'inline';
readMoreBtn.style.display = 'none';
readLessBtn.style.display = 'inline';
});
readLessBtn.addEventListener('click', function () {
truncatedSpan.style.display = 'inline';
fullTextSpan.style.display = 'none';
readMoreBtn.style.display = 'inline';
readLessBtn.style.display = 'none';
});
fragment.appendChild(truncatedSpan);
fragment.appendChild(fullTextSpan);
fragment.appendChild(readMoreBtn);
fragment.appendChild(readLessBtn);
p.appendChild(fragment);
}
});
}); |
||
|
||
} )( jQuery, window, document ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a minor issue with the CSS property
left
being declared twice for.facilities-list li::before
. The second declaration will override the first one. If this is not intentional, you should remove the redundant declaration.