JavaScript Read more on Database result read more from database

Hi there my code is quite simple but Id like for the design purposes to keep everything neat , at the moment Im pulling all the description which is like Some could be huge others can be quite small , anyway to make it fair I decided to make a read more button and once I click it just expand on the text like , SO somehow to make it show the first 160 characters after that ... then ReadMore link button that when you click it expands and shows the whole text Heres my script that I use for now :

<p><?PHP echo $thismovie['description']; ?></p> <div style="text-align:right">

So I would like to know how this is done and if possible only using javascript, thanks !

Answer:1

While you could of course use PHP, another way is to use the text-overflow property of css correctly.

This method will put less strain on the server, especially when there are a bunch of descriptions on the page. Using PHP to concatenate every single one is not efficient and is not the correct way to do this.

Removing a class is much simpler. And you can add it back when you want to show less.

<style>
     .ellipsis {
         white-space: nowrap; 
         text-overflow: ellipsis; 
         overflow: hidden; 
         height: 14px;
     }
     .description {
         width: 300px;
         background: #ccc; 
         padding: 3px;
         margin-top: 30px;
         margin-bottom: 0;
     }
</style>

<!-- It is likely you would use a PHP loop for this but for illustration 
     purposes I've listed them out -->

<p class="description ellipsis"><?=$movie[0]['description']?></p>
<a target='_blank' href="#" class="read-more">Read More</a>

<p class="description ellipsis"><?=$movie[1]['description']?></p>
<a target='_blank' href="#" class="read-more">Read More</a>

<p class="description ellipsis"><?=$movie[2]['description']?></p>
<a target='_blank' href="#" class="read-more">Read More</a>

<!-- use as many as you want with no additional strain on server. -->

WITH JQUERY... http://jsfiddle.net/kx2nbv3z/

<!-- Include jQuery -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
    $(document)
        .on('click','.read-more',function() { 
            $(this).removeClass('read-more').addClass('show-less').html('Show Less').prev('.description').removeClass('ellipsis'); 
        })

        .on('click','.show-less',function() { 
            $(this).removeClass('show-less').addClass('read-more').html('Read More').prev('.description').addClass('ellipsis'); 
        })
    ;
</script>

WITH PURE JAVASCRIPT... http://jsfiddle.net/8wsbw0u8/

<script>


if (document.body.addEventListener) {
    document.body.addEventListener('click',yourHandler,false);
}
else {
    document.body.attachEvent('onclick',yourHandler);//for IE
}

function yourHandler(e) {
    e = e || window.event;
    var target = e.target || e.srcElement;
    prev = target.previousSibling.previousSibling;
    if (target.className.match(/read-more/)) {
        target.className="show-less";
        target.innerHTML = "Show Less";
        prev.setAttribute("class","description");
        console.log(prev);
    }
    else if (target.className.match(/show-less/)) {
        target.className="read-more";
        target.innerHTML = "Read More";
        prev.setAttribute("class","description ellipsis");
    }
}
</script>
Answer:2

EDIT, new code based on Mottie's answer: jQuery.tablesorter.addParser({ id: "monetaryValue", is: function (s) { return false; }, format: function (s) { return s.replace('$'...

EDIT, new code based on Mottie's answer: jQuery.tablesorter.addParser({ id: "monetaryValue", is: function (s) { return false; }, format: function (s) { return s.replace('$'...

  1. jquery tablesorter sorting string
  2. jquery tablesorter sort by data attribute
  3. jquery tablesorter sort icon
  4. jquery tablesorter sort date
  5. jquery tablesorter sort multiple columns
  6. jquery tablesorter sort two columns
  7. jquery tablesorter sort only one column
  8. jquery tablesorter sort
  9. jquery tablesorter sort image
  10. jquery tablesorter sort numbers
  11. jquery tablesorter sort column
  12. jquery tablesorter sort datetime
  13. jquery tablesorter sort by attribute
  14. jquery.tablesorter sort by date time
  15. jquery tablesorter sort alphanumeric
  16. jquery tablesorter sort initial order
  17. jquery tablesorter sort on hidden column
  18. jquery tablesorter sort on load
  19. jquery tablesorter sort checkbox
  20. jquery tablesorter sort not working

Sorry, for that (but I´m a regex noob) in js how would one replace the first folder in a url string. Pseudo Code: Replace at start of string: /any-amount-of-characters/ with nothing Examples $...

Sorry, for that (but I´m a regex noob) in js how would one replace the first folder in a url string. Pseudo Code: Replace at start of string: /any-amount-of-characters/ with nothing Examples $...

  1. regex replace first occurrence
  2. regex replace first character
  3. regex replace first and last character
  4. regex replace first group
  5. regex replace first occurrence python
  6. regex replace first line
  7. regex replace first character if
  8. regex replace first 5 characters
  9. regex replace first character with uppercase
  10. regex replace first letter to uppercase
  11. regex replace first occurrence of character
  12. regex replace first space with comma
  13. regex replace first character lowercase
  14. regex replace first capture group
  15. regex replace first occurrence c#
  16. regex replace first n characters
  17. regex replace first two characters
  18. regex replace first 3 characters
  19. regex replace first tab
  20. regex replace first comma

What are the scenarios when you want to use CDN to load a javascript file? And which files you might want to lazy load. Or perhaps which scripts are the best candidate to be combined and minified? ...

What are the scenarios when you want to use CDN to load a javascript file? And which files you might want to lazy load. Or perhaps which scripts are the best candidate to be combined and minified? ...

I'm working on a jQuery plugin that does not have a selector. When initializing it, I instanciate an object that has functions. In these functions, I need to use closures. In these closures, I would ...

I'm working on a jQuery plugin that does not have a selector. When initializing it, I instanciate an object that has functions. In these functions, I need to use closures. In these closures, I would ...

  1. call javascript class from html
  2. call javascript class function
  3. call javascript class
  4. call javascript class method
  5. javascript call class from another file
  6. javascript call class method from another method
  7. javascript call class method by string
  8. javascript call class constructor
  9. javascript call class method by name
  10. javascript call class method from constructor
  11. javascript call class method from callback
  12. javascript call class without new
  13. javascript call class static method
  14. javascript call class by string
  15. javascript call class function from string
  16. javascript call class method from html
  17. javascript call class function by name
  18. javascript call class function from event
  19. javascript call class css
  20. javascript call class attribute