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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:44:54+00:00 2026-06-03T08:44:54+00:00

I have a html structure in my webpage as below : <div class=group_name>Group A</div>

  • 0

I have a html structure in my webpage as below :

<div class="group_name">Group A</div>
<div class="student_name">Studen 1</div>
<div class="student_name">Studen 2</div>
<div class="student_name">Studen 3</div>

<div class="group_name">Group B</div>
<div class="student_name">Studen 1</div>
<div class="student_name">Studen 2</div>
<div class="student_name">Studen 3</div>

..
.
.

Now this structure is dynamically generated using PHP. What I want to do is, I want to add below code as a last element of “Group A” :

<div class="student_name">Studen 4</div>

But I want to add it using jQuery, as I am getting this “Student 4” record as a Ajax response string. So the div is getting added based on the Group Name. At the moment I have used static value for group name as “Group A”. So the Javascript will be something like below :

$(".group_name").each(function(index){
    var group_name = $(this).text();

    if(group_name=='Group A'){
          // Add Student 4 to last position of this group
    }
});

So this way I am already selecting the “Group A” element. But now I want to add “Student 4” as the last entry in that group (below “Student 3”).
How can I do 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-03T08:44:57+00:00Added an answer on June 3, 2026 at 8:44 am
    $('.group_name').filter(function() {
        return $(this).text() == 'Group A';
    }).nextUntil('.group_name').filter('.student_name').last().after('<div class="student_name">Student X</div>');
    

    What this code does:

    • $('.group_name') select all groups
    • .filter(...) reduces it to the element with the correct group name. I did not use :contains() as that would perform a substring match instead of an exact match.
    • .nextUntil('.group_name') takes all following siblings which are not a new group
    • .filter('.student_name') removes all elements that are not student elements – just in case your DOM structure is bad enough to make this necessary
    • .last() reduces it to the last one – the place after which the new one should be inserted
    • .after(...) inserts the new entry after that element.

    Here is a demo: http://jsfiddle.net/ThiefMaster/5UHgC/


    Please note that you could make this much easier, cleaner and more structured by changing your structure like this:

    <div class="group" id="group-1">
        <div class="group_name">...</div>
        <div class="students">
            <div class="student_name"></div>
            <div class="student_name"></div>
            <div class="student_name"></div>
        </div>
    </div>
    
    <div class="group" id="group-2">
        <div class="group_name">...</div>
        <div class="students">
            <div class="student_name"></div>
            <div class="student_name"></div>
            <div class="student_name"></div>
        </div>
    </div>
    

    Then you could simply use this:

    $('#group-X .students').append('<div class="student_name"></div>');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this html structure <div class=top_menu> <ul> <li class=left><a href= class=family-filter>asdas</a></li> <li class=welcome>Welcome
Assuming I have this HTML structure: <ul class='menu'> <li> <div></div> <div> <div></div> <div> <a
i have html structure like this <div> <span class=ic> <span class=ic>12345</span> </span> </div> i
I have this html structure: <div id=home> <div class=section_header> <h1 class=left data-target=newest><?=lang('home_newest');?></h1> ... </div>
I have this HTML structure: <div class=start> <div class=someclass> <div class=catchme> <div=nested> <div class=catchme>
I have a html structure like this: <div class=one> <div id=two> <h2>Any</h2> <h4>Any</h4> </div>
I have this HTML structure and want to convert it to an accordion. <div
I have this HTML structure (excerpt): <td> <select id=test1 class=pub_chooser> <option value=99>All Publications</option> <option
I have a html structure like this.. <div id=divid> <ul id=ulid> <li style=margin-left: 8px>
i have following HTML structure: <div class=container> <div class=sidebar></div> <div class=content></div> <div class=subscript></div> </div>

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.