0

I have an html code as

<div class="container">
    <div class="ax" > <p>some text ... </p> </div>
    <div class="ax" > <p>some text ... </p> </div>
    <div class="ax" > <p>some text ... </p> </div>
    <div class="ax" > <p>some text ... </p> </div>
</div>

Now I need to check each specific "p" in the div classed "ax" if it has more than 175 characters, if it does, it need to show "read more" text in the end.

My main problem is that I can't use id's because these divs will be auto generated.

Please help.

1
  • Adding the correct tags is a key part of asking a question. If you are looking for a jQuery answer then it is always wise to add that to the tag list :) Commented Jan 31, 2016 at 15:12

3 Answers 3

1

Get the text of each paragraph, check the length, and use a condition to return the "read more" text

$('.ax p').text(function(_, txt) {
   return txt.length > 175 ? txt.slice(0,175) + '... read more ...' : txt;
});
Sign up to request clarification or add additional context in comments.

Comments

0

In order to access each of the `ax' elements, just do:

   $(".ax > p").each( function(index, elem) {
        // elem now is one of the "p" tags with "ax" class elements
        if ( $(elem).html().length > 175 ....

   });

the callback will be called for each of the elements found. See: http://api.jquery.com/each/

Comments

0
$('.ax').each(function() {
  if($(this).find('p').text().length > 175){
    //redmore();
  }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.