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

  • SEARCH
  • Home
  • 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 8023833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:40:55+00:00 2026-06-04T22:40:55+00:00

This should be fairly simple, but I seem to be missing something. I’m working

  • 0

This should be fairly simple, but I seem to be missing something. I’m working on a little project in which the code that I’m writing aims to do a few things across the document. Here’s the HTML code that I want to apply my script’s actions to:

<div class="entry">
    <p>First paragraph</p>
    <p>Second paragraph</p>
    <p>Third paragraph</p>
    <p>One more...</p>

    <div class="bttn"><a href="#">Click for More</a></div>
</div>

This template (that is, several p elements and one div with the class “bttn” inside a wrapper div with the class “entry”) is repeated multiple times across the document. Users add content to the paragraphs and eventually publish it to the site. And here’s what I wish to do in my .js file:

  1. Count all p elements inside the div elements with the class
    “entry”.
  2. Throughout the entire document, if there are more than two p elements inside each div classed “entry”, I want to leave the first two untouched and move the rest to a new div element with the class “slider”. This new element would be placed after the “Click for More” button.
  3. Add the class “show” to the already existent div element classed “bttn”, but only to those that belong to the classes “entry” that contained more than two paragraphs and were moved to the new div with the class “slider”. The new button would look like this: div class=”bttn slider” (plus the anchor tag and everything else as shown in the template).

So basically, if the user who published the content added more than two paragraphs to the HTML template, when the doc is ready I want the script to take the other paragraphs, move them to a new div element, and add a new class to the button. In the end when the “Click for More” button is clicked, the extra paragraphs that were removed and inserted afterwards will fade in nicely (since I’ll keep them hidden under the button once I’ve re-inserted them). Here’s what I’ve done so far:

$('.entry').each(function () {
    $counter = $('.entry p').length;

    if ($counter > 2) {
        $removeExtra = $(this).find('p').not(':eq(1)').not(':eq(0)').detach();

        $(this).find('.bttn').after(function () {
            return '<div class="slider">' + $removeExtra.html() + '</div>';
        }).addClass('show');
    }
});

Issues:

  • We all know that the html() method will only return the first set
    of matched elements, so on button click I only get the first
    paragraph that was removed from each class “entry”, and I need to get
    back all detached paragraphs that I stored in the variable
    $removeExtra.
  • Every div element with the class “bttn” is getting the new class “show”, and I only want this to happen to those that belong to the classes “entry” that saw some extra paragraphs removed. The div class=”entry” elements that had two paragraphs from the beginning should see no modification in the button’s class.

Would greatly appreciate any help with this. Thanks in advance!

  • 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-04T22:40:56+00:00Added an answer on June 4, 2026 at 10:40 pm

    This line:

    $counter = $('.entry p').length;
    

    Is finding all paragraphs within any “.entry” div, when you want paragraphs just within the current “.entry” div. Also you don’t need to use the callback-function syntax with .after() since you’re using it to add after only one element at a time.

    Try this instead:

    $('.entry').each(function () {
        var $this = $(this),
            $p = $this.find('p');
    
        if ($p.length > 2) {
            $('<div class="slider"/>').appendTo(this).append($p.slice(2));
            $this.find('.bttn').addClass('show');
        }
    });
    

    I’ve used the .append() method rather than .after() since .append() will automatically add them after the last child of the element you’re appending to. You don’t need to detach the paragraphs before appending them to the new div because .append() will move them (at least, it will move them when there is only one target element – as is the case here).

    I’ve used .slice() rather than your .not(':eq(1)').not(':eq(0)').

    Note that you should declare variables with var or they become globals.

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

Sidebar

Related Questions

This seems like it should be something fairly simple, but I just can't seem
I'm trying to do something that I think should be fairly simple but I've
I am trying to do something that should be fairly simple but ISNULL isn't
I want to do something that seems like it should be fairly simple, but
This should be a fairly simple question but I know very little about SQL.
This should be painfully simple, but I cannot come up with a working connection
I know this should be fairly simple but I don't get it anyhow. I've
This should turn out to be a fairly simple Regex, but I'm no expert
It feels like this should be fairly simple but nothing i've tried so far
This should be fairly simple, but it is turning out to be more complicated

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.