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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:31:40+00:00 2026-06-15T05:31:40+00:00

I have multiple divs with the same class name that dynamically populate a container.

  • 0

I have multiple divs with the same class name that dynamically populate a container.

<div class="container">
<div class="content cancel"><button class="showModal"></button></div>
<div class="content cancel"><button class="showModal"></button></div>
<div class="content cancel"><button class="showModal"></button></div>
<div class="content cancel"><button class="showModal"></button></div>
<div class="content cancel"><button class="showModal"></button></div>
</div>

Each div.content has a button that will show a jqueryUI dialog. Once the modal is launched, it has two buttons for save and cancel. If cancel is clicked, an alert will show the index position of the div that launched the modal and remove the class “cancel” from the div.content container it was launched from.

<div class="modal">
<button class="save">save</button>
<button class="RemoveCancel">cancel</button>
</div>

Here is my Jquery

$(".modal").on("click", ".RemoveCancel", function () {

    var parent = $(this).closest(".content");
    var modalTest = $(".content").index(parent);
    $("div.content:nth-child(" + (modalTest + 1) + ")").removeClass("cancel");
    alert("div.content:nth-child(" + (modalTest + 1) + ")");
});

and for the modal

$(function () {
    $(".modal").dialog({
        autoOpen: false,
        height: 140,
        modal: true
    });

 $(".showModal").live('click', function () {
        $(".modal").dialog("open");
        return false;
    });

    $(".save, .RemoveCancel").live('click', function () {
        $(".modal").dialog("close");
        return false;
    });
});

Thanks for any input, currently i’m getting a -1 value for the alert. If I leave out the selector for index it’ll show however many divs are apart of the index. I hope this makes sense, thank you again.

  • 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-15T05:31:41+00:00Added an answer on June 15, 2026 at 5:31 am

    I would restructure your code. This is the framework that I came up with. I think that you can leverage it for your needs.

    http://jsfiddle.net/v4dtL/

    A few things:

    Don’t use live. It has been deprecated. Use on or delegate instead.

    Add a class called modalized to the clicked element. This allows you to keep track of who launched the modal window without doing any funky DOM traversals.

    Use jQuery’s index to find out the index of the clicked element in relation to its siblings.

    //when showmodal is clicked, add modalized class to the clicked element
    $(".container").on('click', '.showModal', function() {
        $(".modal").show();
        $(this).addClass('modalized');
    });
    
    //I broke save and removecancel into separate event handlers so that they don't step on each others toes (order of operations)
    $(".modal").on('click', '.save', function() {
        $(".modal").hide();
        $('.modalized').removeClass('modalized');
    });
    
    //Use Index() to find the modalized element in relation to its siblings
    $(".modal").on("click", ".RemoveCancel", function() {        
        var index = $('.showModal').index($('.modalized'));
        alert(index);
        $(".modal").hide();    
        $('.modalized').removeClass('modalized');
    });
    
    ​
    

    jQuery’s data object allows you to store information on an element. So you could do something like this. Untested.

    $('.showModal').click( function() {
          //store clicked index
          var index = $(this).siblings().index($(this));
          $('.modal').show().data('clickedIndex', index);
    });
    
    $('.cancel').click( function() {
          //retrieve clicked index
          var index = $(this).closest('.modal').data('clickedIndex');
          alert(index);
          //get all showmodal buttons, but grab the one with matching index
          $('.showModal')[index].removeClass('foo');
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple input text fields grouped in divs: <div class=container0> <input class=containerItem0 name=containerItem[0][0]
I have multiple divs in my body which need the same style: <div class=box></div>
I have multiple divs (haveing same class names). I want to move the div
I have multiple div's with the same class that are on a page that
I have multiple toggle divs on the same page, and the div I'm toggling
I have multiple divs like this: <div class=small></div> <div class=small></div> <div class=small></div> <div class=small></div>
I have multiple <div> tag with same width but different height in a container.
so I have multiple divs of the same class below each other. I now
i have multiple divs with the same class on my page. I want to
I have a page that looks something like this: ... <div class=container> <div class=info>

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.