0

I have a row of 5 buttons which has been implemented in the fiddle below. I want to implement the code in the way that if i click on any button e.g. Water a list of specific button appears vertically.Similarly a list of buttons should appear for all the 5 buttons and at one time there should be only one list visible.

<div id="outer">
        <div class="inner"><button type="submit" class="msgBtn2" onClick="return false;" >Water</button></div>
        
        <div class="inner"><button type="submit" class="msgBtn2" onClick="return false;">Public</button></div>
        
        <div class="inner"><button class="msgBtn2">Transport</button></div>
        
          <div class="inner"><button type="submit" class="msgBtn2" onClick="return false;">House</button></div>
          
          
            <div class="inner"><button type="submit" class="msgBtn2" onClick="return false;">Utilities</button></div>
            
    </div> 

Here is the fiddle : http://jsfiddle.net/fku50o9v/

How can i do that? I tried implementeing it with OnClick but was unable to do so.

5
  • Using the editor you can actually create a runnable example without needing to link fiddle. It is the square icon with the <>. Commented Jun 1, 2021 at 15:37
  • What did you try? what error did you get? Add your JS code. Commented Jun 1, 2021 at 15:42
  • @Bulent his JS is in the fiddle example, but edit queue is full so I can't make the edit =/ Commented Jun 1, 2021 at 15:43
  • In the fiddle, there is no JS code. Commented Jun 1, 2021 at 15:44
  • @Bulent I am going crazy, disregard. Commented Jun 1, 2021 at 15:46

1 Answer 1

1

function popup(id){
if($("#"+id).hasClass( "vis" )){
   $("#"+id).removeClass( "vis" );
}else{
  $(".dropdown-content").removeClass( "vis" );
  $("#"+id).addClass( "vis" );
  }
}
#outer
{
    width:100%;
    text-align: center;
}
.inner
{
    display: inline-block;
    padding-right: 20px;
}

    
.msgBtn2{
    cursor: pointer;
    font-size: 1.2rem;
    height: 2.5rem;
    border: none;
    border-radius: 10px;
    color: blue;
    background-color: #ffff;
    outline: none;
    box-shadow: 0 0.3rem  rgba(121,121,121,0.70);
    
}
.dropdown {
  position: relative;
  display: inline-block;
  width: 100%;
}

.dropdown-content {
  min-width: 160px;
  top: 50px;
  margin-left: -40px;
  display: none;
  position: absolute;
  z-index: 1;
  color: red;
}
.dropdown-content button{
  color: red;
  margin: 5px
}

.vis {
  display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="outer">
        <div class="inner">
        <div class="dropdown">
  <span><button class="msgBtn2" onclick="popup('water');" >Water</button></span>
  <div id="water" class="dropdown-content">
  <button type="submit" class="msgBtn2">Button One</button> 
  <button type="submit" class="msgBtn2">Button Two</button> 
  <button type="submit" class="msgBtn2">Button Three</button> 
  </div>
</div></div>
        
        <div class="inner">
        <div class="dropdown">
  <span><button class="msgBtn2" onClick="popup('public');" >Public</button></span>
  <div id="public" class="dropdown-content">
  <button type="submit" class="msgBtn2">Button One</button> 
  <button type="submit" class="msgBtn2">Button Two</button> 
  <button type="submit" class="msgBtn2">Button Three</button> 
  <button type="submit" class="msgBtn2">Other Button</button> 
  </div></div>
  </div>
        
        <div class="inner">
        <div class="dropdown">
  <span><button type="submit" class="msgBtn2" onClick="popup('transport');" >Transport</button></span>
  <div id="transport" class="dropdown-content">
  <button type="submit" class="msgBtn2">Button One</button> 
  <button type="submit" class="msgBtn2">Button Two</button> 
  <button type="submit" class="msgBtn2">Button Three</button> 
  <button type="submit" class="msgBtn2">Other Button</button> 
  </div></div></div>
        
        <div class="inner">
          <div class="dropdown">
  <span><button type="submit" class="msgBtn2" onClick="popup('house');" >House</button></span>
  <div id="house" class="dropdown-content">
  <button type="submit" class="msgBtn2">Button One</button> 
  <button type="submit" class="msgBtn2">Button Two</button> 
  <button type="submit" class="msgBtn2">Button Three</button> 
  <button type="submit" class="msgBtn2">Other Button</button> 
  </div></div></div>
          
        <div class="inner">
            <div class="dropdown">
  <span><button type="submit" class="msgBtn2" onClick="popup('utilities');" >Utilities</button></span>
  <div id="utilities" class="dropdown-content">
  <button type="submit" class="msgBtn2">Button One</button> 
  <button type="submit" class="msgBtn2">Button Two</button> 
  <button type="submit" class="msgBtn2">Button Three</button> 
  <button type="submit" class="msgBtn2">2 Other Button</button> 
  </div></div></div>
    </div>

Try this!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.