Reputation: 1038
i stared writing Jquery plugin and also try to learn Jquery plugin development best practices.
what i need to do is when i click more
need to show full content and also if click again hide it. could you please help me to do.
(function() {
$.fn.textCounter = function(className, textCount) {
return this.each(function() {
var title = $('.' + className).text();
if (title.length > textCount) {
var content = title.substr(0, textCount) + ' ....';
$('.' + className).text(content).append('<span class="showcontent">More <i class="fa fa-angle-down" aria-hidden="true"></i></span>');
} else {
$('.' + className).text(title);
}
});
};
}());
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jQuery Word/Text Counter Plugins</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<!-- Start second row -->
<section class="details-row-second">
<div class="container content-box">
<div class="col-xs-12 col-sm-6 col-md-6">
<h2>WHO WE ARE ?</h2>
<p class="contentClass">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with
desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</div>
<div class="hidden-xs col-sm-6 col-md-6">
</div>
</div>
</section>
</body>
<script type="text/javascript">
$(document).ready(function() {
$(".contentClass").textCounter('contentClass', '300');
});
</script>
</html>
Upvotes: 0
Views: 84
Reputation: 74738
Do not send the number as a string:
$(".contentClass").textCounter('contentClass', 300);
Because in your plugin you are comparing a number to a string:
if (title.length > textCount) {
Here title.length
's type is number and textCount
was string now it is a number.
Upvotes: 1