How can I modify my jQuery code to wrap the middle text in an h2 tag?
I am currently using a code snippet from code-tricks. You can find the original code snippets here:
$(document).ready(function() {
var showChar = 100;
var ellipsestext = "...";
var moretext = "Show more >";
var lesstext = "Show less";
$('.more').each(function() {
var content = $(this).html();
if (content.length > showChar) {
var c = content.substr(0, showChar);
var h = content.substr(showChar, content.length - showChar);
var html = c + '<span class="moreellipses">' + ellipsestext + ' </span><span class="morecontent"><span>' + h + '</span> <a href="" class="morelink">' + moretext + '</a></span>';
$(this).html(html);
}
});
$(".morelink").click(function() {
if ($(this).hasClass("less")) {
$(this).removeClass("less");
$(this).html(moretext);
} else {
$(this).addClass("less");
$(this).html(lesstext);
}
$(this).parent().prev().toggle();
$(this).prev().toggle();
return false;
});
});
.morecontent span {
display: none;
}
.morelink {
display: block;
}
.productdescription {
font-weight: 400;
}
.productdescription h2 {
font-weight: normal;
font-size: 13px;
margin: 0px;
padding-top: 24px;
}
.productdescription strong {
padding-bottom: 24px;
float: left;
width: 100%;
}
.productdescription ul {
list-style-type: square;
padding: 24px 0px 0px 15px;
}
.morecontent span {
display: none;
}
.morelink {
display: block;
}
.morelink {
color: #4285f4;
display: inline-block;
}
.morelink:hover {
text-decoration: underline;
}
.less {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<span class="productdescription more">
<strong>Produktdetails</strong>
Change this text to a headline h2. Change this text to a headline h2. Change this text to a headline h2. Change this text to a headline h2. Change this text to a headline h2. Change this text to a headline h2. Change this text to a headline h2.
<ul>
<li>Lorem ipsum dolor sit amet</li>
<li>Lorem ipsum dolor sit amet</li>
<li>Lorem ipsum dolor sit amet</li>
<li>Lorem ipsum dolor sit amet</li>
<li>Lorem ipsum dolor sit amet</li>
<li>Lorem ipsum dolor sit amet</li>
<li>Lorem ipsum dolor sit amet</li>
<li>Lorem ipsum dolor sit amet</li>
</ul>
</span>