Solved

length of text inside a span - and handle long text gracefully

Posted on 2013-01-24
3
273 Views
Last Modified: 2013-01-30
How do I find the length of the text inside of a <span> tag?


I tried:

alert($('span[id*="lblDetails"]').length);

and it said "2" and I know that is not correct.


Essentially, what I want to do is if the text inside the span is greater than a certain length, I want to put "..." and then when they hover over the truncated text I want to show a popup  bubble of some sort that shows the full text.
0
Comment
Question by:knowlton
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 400 total points
ID: 38816098
alert($('span[id*="lblDetails"]').length); // display how mayn object jQUery found the provided selector : span[id*="lblDetails"]
If you get 2, that mean you've two objects...

To display the length if there's only ONE :
$('span[id*="lblDetails"]').text().length

Open in new window

To display the length if there's MORE THAN ONE (your current case) :
var total = 0;
$('span[id*="lblDetails"]').each(function() {
     var text = $(this).text();
     alert( "text is " + text );
     alert( "text length " + text.length );
     total = total + text.length;
     alert( "total is " + total );
})
alert( "last total is " + total )

Open in new window

0
 
LVL 22

Assisted Solution

by:Mrunal
Mrunal earned 100 total points
ID: 38818779
Another way is:

$('span[id*="lblDetails"]').html().length
0
 
LVL 5

Author Closing Comment

by:knowlton
ID: 38837545
Thank you.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
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 how to dynamically set the form action using jQuery.
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now