Dotdotdot.js after and callback features - can't hide a link
Greetings,
I'm trying to use dotdotdot.js to ellipsis-truncate text in divs and other containers. Using dotdotdot I can specify a container I want to receive ellipsis-truncation:
- $(document).ready(function() {
- $(".truncated_box").dotdotdot({
- after: "a.readMoreLink",
- callback: hideLinkIfNoTruncation
- });
- });
dotdotdot will truncate text inside containers with class truncated_box, and add an ellipsis and a link with the class readMoreLink. However, a.readMoreLink must initially be inside the container, like this:
- <div class="truncated_box">
- This is a rather lengthy text inside a div. This is a rather lengthy text inside a div. This is a rather lengthy text inside a div.
- <a href="#" class="readMoreLink" onclick="clicked()">Read More</a>
- </div>
The given callback is called whenever a container with the given class is considered for truncation (regardless of whether truncation occurs or not). So I wrote this callback so ensure that if a container is considered for truncation but
not truncated the 'read more' link will be hidden:
- function hideLinkIfNoTruncation(isTruncated, origContent) {
- if (!isTruncated) {
- var shouldBeAnAnchor = origContent.filter('a')
- shouldBeAnAnchor.hide()
- }
- }
The hide call has no effect. If I debug I see that origContent.filter('a')
does return the <a> object. What is wrong with my code? Bottom line is,
I only want the 'read more' link to show if truncation actually happens!
If anyone can help, thanks. I've attached a zipfile with my HTML plus jquery and dotdotdot.js. Also, here is all the code from my HTML page:
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <meta charset="utf-8">
- <title>Dot Dot Dot - Does it Work?</title>
- <script src="js/jquery-1.8.3.js" type="text/javascript"></script>
- <script src="js/jquery.dotdotdot-1.5.2.js" type="text/javascript"></script>
- <script type="text/javascript">
- function hideLinkIfNoTruncation(isTruncated, origContent) {
- if (!isTruncated) {
- var shouldBeAnAnchor = origContent.filter('a')
- shouldBeAnAnchor.hide()
- }
- }
- $(document).ready(function() {
- $(".truncated_box").dotdotdot({
- after: "a.readMoreLink",
- callback: hideLinkIfNoTruncation
- });
- });
- function clicked() {
- alert("Link was clicked.");
- }
- </script>
- <style>
- div.truncated_box {
- height: 135px;
- width: 350px;
- font-size: 18px;
- margin: 5px;
- padding: 3px;
- border-style: solid;
- border-width: 3px;
- border-color: red;
- }
- </style>
- </head>
- <body>
- <h1>Three Dots</h1>
- <h3>Ellipses and Read More Link</h3>
- <div class="truncated_box">
- This is a rather lengthy text inside a div. This is a rather lengthy text inside a div. This is a rather lengthy text inside a div. This is a rather lengthy text inside a div. This is a rather lengthy text inside a div. This is a rather lengthy text inside a div.
- This is a rather lengthy text inside a div. This is a rather lengthy text inside a div. This is a rather lengthy text inside a div. This is a rather lengthy text inside a div. This is a rather lengthy text inside a div. This is a rather lengthy text inside a div.
- This is a rather lengthy text inside a div. This is a rather lengthy text inside a div. This is a rather lengthy text inside a div. This is a rather lengthy text inside a div. This is a rather lengthy text inside a div. This is a rather lengthy text inside a div.
- <a href="#" class="readMoreLink" onclick="clicked()">Read More</a>
- </div>
- <div class="truncated_box">
- This is a rather lengthy text inside a div. This is a rather lengthy text inside a div. This is a rather lengthy text inside a div.
- <a href="#" class="readMoreLink" onclick="clicked()">Read More</a>
- </div>
- <div class="truncated_box">
- This is a rather lengthy text inside a div. This is a rather lengthy text inside a div. This is a rather lengthy text inside a div. This is a rather lengthy text inside a div. This is a rather lengthy text inside a div. This is a rather lengthy text inside a div.
- <a href="#" class="readMoreLink" onclick="clicked()">Read More</a>
- </div>
- </body>
- </html>