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 8063139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:55:18+00:00 2026-06-05T10:55:18+00:00

I have a varying number of elements with the same class name: <p class=test></p>

  • 0

I have a varying number of elements with the same class name:

<p class="test"></p>
<p class="test"></p>

I want to replace each of these with a new set of elements with the same class name, but there could be more or fewer elements:

<p class="test"></p>

or

<p class="test"></p>
<p class="test"></p>
<p class="test"></p>

If I use $('p.test').replaceWith('new content'), it will replace each element with the content so I can’t do that.

What is the best way to accomplish this?

EDIT (another explanation):

So I have these elements on the page:

<p class="test"></p>
<p class="test"></p>
<p class="test"></p>

Then, I dynamically retrieve a new set of elements (a varying number of them) that have the same class name (and maybe some other class names). For example:

<p class="test classa"></p>
<p class="test classb"></p>

In the above example, I want the first three <p> elements to be gone. And in their place should be the 2 dynamically retreived <p> elements.

Keep in mind the number of elements of either set can vary.

  • 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-05T10:55:21+00:00Added an answer on June 5, 2026 at 10:55 am

    Updated:

    assuming you have your <p>'s into a container

    <div id="container">
        <p class="test"></p>
        <p class="test"></p>
        <p class="test"></p>
        <p class="test"></p>
    </div>
    

    You could delete them, and append (or prepend) the new elements

    $('p.test').remove(); //Will remove all <p>'s with class test
    

    I assume your new set of elements are into the data array

    var data= { 
      '1': '<p class="newclass">Hello</p>', 
      '2': '<p class="newclass2">Hello2</p>' 
      '3': '<p class="newclass3">Hello3</p>' 
      }; //Could have 100 rows, it wouldn't matter
    
    $.each(data, function(i,val){   
            //Do some stuff
            $('#container').append(val);
      });  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several number inputs of varying lengths which I want to style such
I have a virtual datagridview that I want to set varying row heights for.
I have approximately 5,000 matrices with the same number of rows and varying numbers
I have a text file containing ~300k rows. Each row has a varying number
I have a varying number of inline-block divs that I want to collectively take
I want to pass a varying number of parameters between two objects. I have
I have a number of vector containers of varying size each containing doubles. I
I have a dynamically generated page with varying number of items (div.item). Each item
I have a large data set of GPS collar locations that have a varying
I'm trying to set up a menu. Because this menu can have a varying

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.