?
Solved

jquery function to count

Posted on 2009-12-22
6
Medium Priority
?
371 Views
Last Modified: 2012-05-08
i am trying to truncate the text that's greater than 10.
some text is with in link and some are plain.
my calculation is going wrong, as it doesnt work where text is not with in link. (aaaaaaaaaaaaaaaaaaaaaaaaa)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
 <head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
  $(('.glue a') || ('.glue:not(a)')).each(function(){
        var st = $(this).html();
        if (st.length >= 10) {
            alert(st.length);
            var newst = st.substr(0, 10) + '...';
            $(this).html(newst);
        }
    });    
});
</script>

</head>
<body>
   <span class="glue">aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</span><br /><br />
   <span class="glue"><a href="http://www.yahoo.com">bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb</a></span>
 </body>
</html>

Open in new window

0
Comment
Question by:yingwho
  • 4
  • 2
6 Comments
 
LVL 18

Accepted Solution

by:
bruno earned 2000 total points
ID: 26109765
this should give you what you need. i'd probably refactor a bit and break the string truncation out to it's own function as opposed to having the redundant code in there.
$('.glue').each(function(){
    if ( $(this).children().length > 0 ) {
        
        var st = $(this).children().html();
        if (st.length >= 10) {
            console.log("len:", st.length);
            var newst = st.substr(0, 10) + '...';
            $(this).children().html(newst);
        }
    } else {
        
        var st = $(this).html();
        if (st.length >= 10) {
            console.log("len:", st.length);
            var newst = st.substr(0, 10) + '...';
            $(this).html(newst);
        }
    }
});

Open in new window

0
 

Author Comment

by:yingwho
ID: 26109983
hi bruno

did  u test ur code? it doesnt truncate anything. pl verify.
0
 

Author Comment

by:yingwho
ID: 26109993
hi bruno

i tested ur code , it doesnt work. see attached and let me know the issue.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
 <head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
			$('.glue').each(function(){
				if ( $(this).children().length > 0 ) {
					
					var st = $(this).children().html();
					if (st.length >= 10) {
						console.log("len:", st.length);
						var newst = st.substr(0, 10) + '...';
						$(this).children().html(newst);
					}
				} else {
					
					var st = $(this).html();
					if (st.length >= 10) {
						console.log("len:", st.length);
						var newst = st.substr(0, 10) + '...';
						$(this).html(newst);
					}
				}
			});
    }); 
</script>

</head>
<body>
   <span class="glue">aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</span><br /><br />
   <span class="glue"><a href="http://www.yahoo.com">bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb</a></span>
 </body>
</html>

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Closing Comment

by:yingwho
ID: 31669264
bruno it works, i commented the console code. thanks
0
 
LVL 18

Expert Comment

by:bruno
ID: 26111405
Ah, so sorry about leaving the console code in there!
0
 

Author Comment

by:yingwho
ID: 26290432
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question