?
Solved

jquery

Posted on 2010-01-11
2
Medium Priority
?
380 Views
Last Modified: 2012-05-08

i am trying to grab the text inside the tags and truncate the text to 10 characters.
it fails in 3rd and 4th item as it has nested children tags inside. pl suggest.
<!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) {
										alert(st);
										alert(st.length);
                                                var newst = st.substr(0, 10) + '...';
                                                $(this).children().html(newst);
                                        }
                                } else {
                                        
                                        var st = $(this).html();
                                        if (st.length >= 10) {
										alert(st);
										alert(st.length);
                                                var newst = st.substr(0, 10) + '...';
                                                $(this).html(newst);
                                        }
                                }
                        });
    }); 
</script>
</head>
<body>
   1.<span class="glue">aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</span><br /><br />
   2.<span class="glue"><a href="http://www.yahoo.com">bbbbbbbbb bbbb  bbbbbbbbbbbbbbbbbbbbbbbbb  bbbbbbbbbbbbbbbbbbbb</a></span><br /><br />
   3.<span class="glue"><span><a href="http://www.yahoo.com">bbbbbbbbb bbbb  bbbbbbbbbbbbbbbbbbbbbbbbb bbbbbbbbbbbbbbbbbbbb</a></span></span><br/><br/>
   4.<span class="glue"><span><div><a href="http://www.yahoo.com">bbbbbbbbb bbbb  bbbbbbbbbbbbbbbbbbbbbbbbb bbbbbbbbbbbbbbbbbbbb</div></a></span></span>

 </body>
</html>

Open in new window

0
Comment
Question by:yingwho
2 Comments
 
LVL 14

Accepted Solution

by:
ajitha75 earned 2000 total points
ID: 26290469
use .text instead of .html()

var st = $(this).children().text();

-Ajitha
0
 

Author Closing Comment

by:yingwho
ID: 31675938
thanks
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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

601 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