I know, this question has been asked several times, so apologies for repeating it, but I can't seem to find a solution to my question in those answers.
I have a selection of text that I want to cut after so many words, then on click, add the rest of the words back in - like an accordian style.
I have sliced the text in two and put them back together again using jQuery, but it's cutting words in half. I'd like it to cut off after the words. I'm not sure if the way I have done it is the best approach, so if anyone has any other ideas, or if they know how to cut off after the word, not in the middle that would be much appreciated.
$(".about__profiles--team .text").text(function() {
var introText = $(this).text().substring(0, 150);
var panelText = $(this).text().substring(150);
$(this).html('<span class="accordion-button">' + introText + '</span><span class="panel">' + panelText + '</span>');
});
$('.accordion-button').click(function() {
$(this).parent('#accordion').toggleClass('open-accordion');
});
.text {
border-bottom: 1px solid black;
padding-bottom: 30px;
margin-bottom: 0;
position: relative;
}
.accordion-button {
transition: ease 0.4s;
cursor: pointer;
outline-style: none;
-webkit-touch-callout: none;
-webkit-tap-highlight-color: rgba(0,0,0,0);
}
.accordion-button:before {
content: '+';
display: block;
color: black;
height: 10px;
width: 10px;
position: absolute;
bottom: 10px;
right: 0;
transform: rotate(90deg);
transition: ease 0.4s;
}
.panel {
display: none;
transition: ease 0.4s;
}
.open-accordion > .accordion-button:before {
transform: rotate(-90deg) !important;
}
.open-accordion > .panel {
display: inline !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="about__profiles--team">
<h3 class="job-title">Title</h3>
<h2 class="name">LOREM IPSUM</h2>
<div class="text" id="accordion">Lorem ipsum dolor sit amet, consectetur adipiscing. Mauris tri accumsan, pulvinar purus a, facilisis ligula. Fusce mi a velit ul, consectetur adipiscing Mauris tristique eu orci ac sodales. Vestibulum id accumsan, pulvinar purus a, facilisis ligula. Fusce mi a velit ultrices, id porta arcu viverra. Nunc pia enim, id egestas dolor bibendum nec.</div>
</div>
Thanks