?
Solved

jquery

Posted on 2010-01-11
2
Medium Priority
?
375 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This article discusses how to implement server side field validation and display customized error messages to the client.
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

770 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