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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:27:57+00:00 2026-05-20T22:27:57+00:00

Please refer to the following codes : <div id=message-1 onclick=javascript:showresponddiv(this.id)> </div> <div id=respond-1 style=display:none;>

  • 0

Please refer to the following codes :

<div id="message-1" onclick="javascript:showresponddiv(this.id)>
</div>
<div id="respond-1" style="display:none;">
</div>
<div id="message-2" onclick="javascript:showresponddiv(this.id)>
</div>
<div id="respond-2" style="display:none;">
</div>

<script type="text/javascript">
function showresponddiv(messagedivid){
    var responddivid = messagedivid.replace("message-", "respond-");
    if (document.getElementById(responddivid).style.display=="none"){
        document.getElementById(responddivid).style.display="inline";
    } else {
        document.getElementById(responddivid).style.display="none";
    }
}
</script>

The codes above already success make the respond div appear when user click on message div. The respond div will disappear when user click on message div again. Now my question is how to make the respond div of 1st message disappear when user click on 2nd message to display the respond div of 2nd message?

  • 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-20T22:27:58+00:00Added an answer on May 20, 2026 at 10:27 pm

    You should give the “respond” divs a common class:

    <div id="respond-1" class="response' style="display:none;"></div>
    

    Then you can get all divs by using getElementsByTagName, compare the class and hide them on a match:

    function hideAllResponses() {
        var divs = document.getElementsByTagName('div');
        for(var i = divs.length; i-- ;) {
            var div = divs[i];
            if(div.className === 'response') {
                div.style.display = 'none';
            }
        }
    }
    

    We cannot use getElementsByClassName, because this method is not supported by IE8 and below. But of course this method can be extended to make use of it if it is supported (same for querySelectorAll). This is left as an exercise for the reader.


    Further notes:

    • Adding javascript: to the click handler is syntactically not wrong but totally unnecessary. Just do:

      onclick="showresponddiv(this.id)"
      
    • If you have to do a lot of DOM manipulation of this kind, you should have a look at a library such as jQuery which greatly simplify such tasks.


    Update: If always only one response is shown and you are worried about speed, then store a reference to opened one:

    var current = null;
    
    function showresponddiv(messagedivid){
        var id = messagedivid.replace("message-", "respond-"),
            div = document.getElementById(id);
    
        // hide previous one
        if(current && current !== div) {
            current.style.display =  'none';
        }   
    
        if (div.style.display=="none"){
            div.style.display="inline";
            current = div;
        } 
        else {
            div.style.display="none";
        }
    }
    

    Edit: Fixed logic. See a DEMO.

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

Sidebar

Related Questions

I refer to this question: Javascript value to PHP with Jquery I tried with
Please help me to understand below code. This is the script for drag and
I would like to solve the following problem using constraints but i actually don't
I can very well understand from this Selectutorial what element/tag based descendant selectors are,
Please let me know how to get the value of the first item in
Please suggest me a good method that can be used to write a stream
I'm trying to make a try/catch method to see if a record exist or
i try to code a login form which passes username and password to a
I am sorry if it seems a silly question to you, but I have
I was told that the language generated by the regular expression: (a*b*)* is regular.

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.