John Davidson

javascript - jQuery set fixed length content in div

0 comments
Message:


i m stuck into one situation, i have a div having some paragraph content. i only want to show 100 words of content in div for example - (sometextjonty) i only want to show 7 alphabets content in div so my div now will show (sometex) text upto here . i only want to do with jquery not with css, any solutions for this. I have no any clue how to do that


echo'<div class="content textlimit"><p>'; the_content(); echo'</p></div>

here above is my content div



My Answer:
You can achieve this by using jQuery to count the number of words in the paragraph and then truncating the content to only show the first 100 words. Here's an example code snippet to achieve this:

javascript
$(document).ready(function() {
$('.textlimit').each(function() {
var content = $(this).find('p').text();
var words = content.split(' ');

if (words.length > 100) {
var truncatedContent = words.slice(0, 100).join(' ');
$(this).find('p').text(truncatedContent);
}
});
});


This code snippet will iterate over each div with the class "textlimit", find the paragraph content inside it, count the number of words, and if it exceeds 100 words, it will truncate the content to only show the first 100 words.

Rate this post

3 of 5 based on 5877 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us