Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8845379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:43:33+00:00 2026-06-14T11:43:33+00:00

I got texts output from PHP/MYSQL, and I used following jQuery to limit the

  • 0

I got texts output from PHP/MYSQL, and I used following jQuery to limit the text and do some show/hide effects. It works fine for the first block, but all blocks are changed to one same content if I loop the items by php, is there a way to make the jQuery applies to each block?

function excerpt(str, nwords) {
    var words = str.split(' ');
    words.splice(nwords, words.length - 1);
    return words.join(' ') + '…';
}

var $div = $('.container');
var excerpt = excerpt($div.text(), 30);

$div.data('html', $div.html()).html( excerpt );

$('button').click(function() {
    var isHidden = $(this).text() == 'Show';
    $div.html( isHidden ? $div.data('html') : excerpt);
    $(this).text( isHidden ? 'Hide' : 'Show' );
});​

HTML:

<div class="container">
    Donec vitae mollis leo. Quisque ut metus ac velit porttitor sollicitudin quis mattis purus. Quisque id risus id sem porta gravida eu nec neque. Praesent non odio nibh, vitae tincidunt sapien. Maecenas rutrum mollis mauris, eu condimentum nunc viverra nec. Donec fringilla imperdiet volutpat. Morbi in sem ante. Phasellus libero purus, viverra a aliquam ac, eleifend eu nisl. Etiam quis odio nec quam rhoncus iaculis ac quis velit. Sed ut lacus quis enim tempor ullamcorper. Donec odio ligula, porta et bibendum aliquam, blandit vitae lacus.
        Donec vitae mollis leo. Quisque ut metus ac velit porttitor sollicitudin quis mattis purus. Quisque id risus id sem porta gravida eu nec neque. Praesent non odio nibh, vitae tincidunt sapien. Maecenas rutrum mollis mauris, eu condimentum nunc viverra nec. Donec fringilla imperdiet volutpat. Morbi in sem ante. Phasellus libero purus, viverra a aliquam ac, eleifend eu nisl. Etiam quis odio nec quam rhoncus iaculis ac quis velit. Sed ut lacus quis enim tempor ullamcorper. Donec odio ligula, porta et bibendum aliquam, blandit vitae lacus.
    <div class="myimage">picture</div>
    <div class="myimage">picture</div>
    <div class="myimage">picture</div>
</div>
<button>Show</button>




<div class="container">
  Curabitur sed mauris at eros viverra fringilla in a ipsum. Aliquam erat volutpat. Integer quis arcu mauris. Cras diam dui, porta non egestas in, auctor ornare nunc. Donec quam ligula, vulputate quis sodales ultricies, gravida vitae augue. Ut ut nibh nisi. Phasellus placerat odio sed mauris accumsan tincidunt. Nunc orci eros, posuere sollicitudin posuere eget, condimentum imperdiet mi. Sed ornare felis sit amet lectus viverra at blandit erat aliquam. Sed laoreet ante consequat elit vestibulum commodo. In sed rutrum lorem. Integer sagittis placerat mi, at tristique nunc consectetur sit amet. Quisque nec nulla eget lacus condimentum iaculis eget id neque. Praesent sagittis, elit non porttitor cursus, justo elit lobortis magna, vitae interdum magna arcu et sem.
    <div class="myimage">picture 3</div>
    <div class="myimage">picture 5</div>
    <div class="myimage">picture 6</div>
</div>
<button>Show</button>
​

SAMPLE: http://jsfiddle.net/BqesP/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:43:34+00:00Added an answer on June 14, 2026 at 11:43 am

    the issue is that you are referencing the entire set of container elements inside your click event handler. So any time any button is clicked, you are applying the function to all .container elements. here’s a quick change to fix it:

    function excerpt(str, nwords) {
        var words = str.split(' ');
        words.splice(nwords, words.length - 1);
        return words.join(' ') + '&hellip;';
    }
    
    var $div = $('.container');
    
    $div.each(function() {
        var theExcerpt = excerpt($(this).text(), 30);
        $(this).data('html', $(this).html()).html( theExcerpt );
    });
    
    $('button').click(function() {
        var isHidden = $(this).text() == 'Show';
        var $div = $(this).prev();
        var theExcerpt = excerpt($div.text(), 30);
        $div.html( isHidden ? $div.data('html') : theExcerpt);
        $(this).text( isHidden ? 'Hide' : 'Show' );
    });​
    

    by getting the target container inside the function, you can make sure you only effect the element you want to be effected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a PHP/Mysql app that lets users post text from a form. When
I've got a python package which outputs considerable help text from: help(package) I would
My input file is a large txt file with concatenated texts I got from
On my page in IE8, I have got some text which gets changed through
I've got a chat program which pushes JSON data from Apache/PHP to Node.js, via
I was having some difficulties with some text I was receiving from a Web
I am getting some info from a https web server using PHP plus cURL.
In OSX (I'm on 10.7.x, Lion), you can grab some text from most any
I've got the following code: <?php echo strip_tags($firstArticle->introtext); ?> Where $firstArticle is a stdClass
I've got lots of text that I need to output, which includes all sorts

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.