search

Home  >  Q&A  >  body text

Can we show/hide different content in a div without using Select method but just with mouse click?

Is it possible to show/hide different content in a div without using the Select method but just with a mouse click on the div?

For example:

.inv{
  display: none;
}
<div class="inv">
 
  <div> Option 1 </div>
  <div> Option 2 </div>
  <div> Option 3 </div>
 
  <div class="inv">Content 1</div>
  <div class="inv">Content 2</div>
  <div class="inv">Content 3</div>

</div>

If I click on the "Option 1" div, it shows everything in "Content 1"

Option 1 -> Content 1

is it possible?

P粉155832941P粉155832941325 days ago458

reply all(1)I'll reply

  • P粉116631591

    P粉1166315912024-02-18 00:07:54

    To do this strictly without using JavaScript, you can use a labeled checkbox, but you must modify the tag:

    .inv {
      display: none;
    }
    
    label{
      display: block;
    }
    
    input {
      display: none;
    }
    
    input:checked + .inv{
      display: block !important;
    }
    
    
    
    
    
    
    Content 1
    Content 2
    Content 3

    If you don't want to make too many changes to the current markup, you have to use JS. The following example uses data attributes to identify element switches.

    $('div[data-for]').click(function() {
      $(`.inv[data-id="${this.dataset.for}"]`).toggle()
    })
    .inv {
      display: none;
    }
    sssccc
    
    Option 1
    Option 2
    Option 3
    Content 1
    Content 2
    Content 3

    reply
    0
  • Cancelreply