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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:12:06+00:00 2026-05-26T02:12:06+00:00

Okay so we have a mySQL while loop echoing out divs for a certain

  • 0

Okay so we have a mySQL while loop echoing out divs for a certain result.

Here is our code:

<div class="post">
<p>hello World</p>
  <div class="contentSlide">
    <p>Members image on left and comments on the right. Date floated on right, and link to user.</p>
    <br />
    <div class="contact">
        <h3>Post a comment below</h3>
        <p>My <label class="agentsHcard">Listings</label> <small>Only displays for logged in</small></p>

       <form>
            <input type="text" READONLY></input><br />
            <textarea id="testTextarea" class="" type="text" name="comment" rows="3" style="background: none repeat scroll 0% 0% rgb(255, 255, 255); border: 1px solid rgb(51, 153, 255);"></textarea><br />
            <div style="float:right;margin-right:40px;">
                <button id="buttonsend" class="medium pink button" value="submit" name="submit" type="submit">Add Comment</button>
            </div>
       </form>
       <br />
    </div>
</div>
<div id="button" class="open">Comments(6) &#x25bc;</div>
</div>

Basically each result has a unique ID in our database, eg: 1, 2, 3, 4, 5

$(document).ready(function(){
  reply=false;
$(".close").live('click',function(){
    if(reply!==true){
    $(".contentSlide, #contact").slideUp();
    $(this).html("Comments(6) \u25bc").removeClass("close").addClass("open");
    }
});
$(".open").live('click',function(){
    $(".contentSlide").slideDown();
    $(this).html("Close \u25b2 <span style='float:right;' onclick='reply=true;'  class='reply' >Post a <a href='javascript:void(0);'>comment</a></span>").removeClass("open").addClass("close");
});
$(".reply").live('click',function(e){

    $("#contact").slideDown(function(){reply=false;});
    $("#button").html("Post and Close \u25b2").removeClass("open").addClass("close");
});
})

We are looking for a way to do this so that each div is given a unique ID, which is passed to the javascript and opens that relevant div.

As it stands, if we have 2 of these results, both of them open when the open class is clicked (obviously).

We know how to do it by duplicating the JS and giving a special ID to each one, but we’re looking for a cleaner way by only using the JS once and making it global.

How can we make it so we only have one piece of javascript, and it runs off an ID? eg:

<div class="contentSlide" id="4">

Thank you.

  • 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-05-26T02:12:07+00:00Added an answer on May 26, 2026 at 2:12 am

    If you want a single piece of Javascript, then id may not be the way to go. In order to operate on DOM elements related to the clicked element, you might want to use relative traversal functions such as closest(selector) to navigate up to the current target’s “.post” div, then traversing back down to the elements to be manipulated:

    $(".close").live('click',function(){
        if(reply!==true){
        $(this).closest(".post").children(".contentSlide, #contact").slideUp();
        $(this).html("Comments(6) \u25bc").removeClass("close").addClass("open");
        }
    });
    

    If you did go with an Id based mechanism, you would probably have to do some string manipulation and have the element id’s be the original id with a prefix/postfix (i.e. button_4, contact_4, etc.)

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

Sidebar

Related Questions

I have a problem with (for me to complicated) MySql query. Okay, here is
Okay I have a series of objects based on a base class which are
Okay I have updated my code quite a bit. I am getting a new
Okay I have updated my code a little, but I am still not exactly
Okay so I have the following Code which appends a string to another in
Okay so I have a scenario similar to the below code, I have a
Okay, here's what I'm trying to do. I am running a MySQL query for
Okay, so I have a MySQL database set up. Most of the tables are
Okay, I have a list of URLs in a MySQL table. I want the
Okay, I am abit confusing here. In my database I have 5 rows of

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.