views:

1593

answers:

4

Can i hide text in a DIV after a number of characters with jQuery?

So far I think it would go something like this - jQuery would cycle through the text until it gets to a certain number of characters. It would then wrap a DIV from that position to the end of the text which could then be hidden with the hide() method. I'm not sure how to insert that wrapping text.

Any other way around this would also be appreciated.

Thanks.

A: 

You may wish to look at the answers here.

Paddy
Its not the physical width I want to limit it by, its the number of characters.
usertest
A: 

If you put a onkeydown event handler on the body tag you can then count the length in the div of interest, and then you can hide it when done, and remove your event handler.

James Black
A: 

I am replacing my original answer with this one.....

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
<script type="text/javascript">
    $(function() {
        var limit = 20;
        var chars = $("#myDiv").text(); 
        if (chars.length > limit) {
            var visiblePart = $("<span> "+ chars.substr(0, limit-1) +"</span>");
            var dots = $("<span class='dots'>... </span>");
            var hiddenPart = $("<span class='more'>"+ chars.substr(limit-1) +"</span>");
            var readMore = $("<span class='read-more'>Read More</span>");
            readMore.click(function() {
                $(this).prev().remove(); // remove dots
                $(this).next().show();
                $(this).remove(); // remove 'read more'
            });

            $("#myDiv").empty()
                .append(visiblePart)
                .append(dots)
                .append(readMore)
                .append(hiddenPart);
        }
    });
</script>
<style type="text/css">
    span.read-more { cursor: pointer; color: red; }
    span.more { display: none;  }
</style>
</head>
<body>
    <div id="myDiv">Here are all<br/> my characters.<br/> I would like to limit them to 20.</div>
</body>
</html>
jessegavin
Your `len` variable is a bit hanging around =)
BalusC
Hi, thanks but unfortunately the DIV text does contain HTML tags as i'm getting it from an external API.
usertest
As far as I can tell there are only <br/> tags in my text, is there any way to skip them in your code? Thanks.
usertest
BalusC. Yup I saw that and removed it. Good catch. +1
jessegavin
The second parameter to substr() is length, not end index, so I'm guessing you actually mean chars.substr(0, limit).
Lobstrosity
substr() doesn't care if the specified length is longer than the string, so for brevity you could just say $("#myDiv").text($("#myDiv").text().substr(0, 15));
Lobstrosity
Excellent! But may I ask one more question. I tried changing myDiv to a class and applying it to multiple DIV's but it won't work. I tried using the each function but that doesn't seem to work. Any idea what the problem could be?
usertest
+1  A: 

This will hide a part of the text

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <title>untitled</title>
    <style type="text/css" media="screen">
     .hide{
      display: none;
     }
    </style>
    <script src="js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript" charset="utf-8">
     $(function(){
      var $elem = $('p');   // The element or elements with the text to hide
      var $limit = 10;  // The number of characters to show
      var $str = $elem.html(); // Getting the text
      var $strtemp = $str.substr(0,$limit); // Get the visible part of the string
      $str = $strtemp + '<span class="hide">' + $str.substr($limit,$str.length) + '</span>'; // Recompose the string with the span tag wrapped around the hidden part of it
      $elem.html($str);  // Write the string to the DOM 
     })
    </script>

</head>
<body>
<p>Some lenghty string goes here</p>
</body>
</html>
zyON
Hi thanks for the reply but it has the same problem as jessegavin's code, HTML tags are not ignored. So sometimes half of a tag shows up in the output.
usertest
I can't comment jessegavin's answer, but here's the way to get it working in multiple divs: Put the jQuery code inside this: $('.someClass').each(function(){ // Code goes here }); and make the change: "#myDiv" To this
zyON
Thanks, finally got it working.
usertest