Equivalent Hide and CSS show

Asked

Viewed 333 times

2

The code below does not run on all browsers:

<button id="btn">
    <img class="" 
         src="/img/icone-formation.png" 
         height="100%" 
         width="100%" 
         onclick="document.getElementById('f1').style.display ='initial'; 
                  document.getElementById('f2').style.display ='none';  
                  document.getElementById('f3').style.display ='none'; 
                  document.getElementById('f4').style.display ='none'; 
                  document.getElementById('f5').style.display ='none';
                  document.getElementById('btn').setAttribute('id', 'press');">
</button>

<button id="btn">
    <img class="" 
         src="/img/icone-formation.png" 
         height="100%" 
         width="100%" 
         onclick="document.getElementById('f2').style.display ='initial'; 
                  document.getElementById('f1').style.display ='none';  
                  document.getElementById('f3').style.display ='none'; 
                  document.getElementById('f4').style.display ='none'; 
                  document.getElementById('f5').style.display ='none';
                  document.getElementById('btn').setAttribute('id', 'press');">
</button>

<button id="btn">
    <img  class="" 
          src="/img/icone-formation.png" 
          height="100%" 
          width="100%" 
          onclick="document.getElementById('f3').style.display ='initial'; 
                   document.getElementById('f1').style.display ='none';  
                   document.getElementById('f2').style.display ='none'; 
                   document.getElementById('f4').style.display ='none'; 
                   document.getElementById('f5').style.display ='none';
                   document.getElementById('btn').setAttribute('id', 'press');">
</button>

<button id="btn">
    <img class="" 
         src="/img/icone-formation.png" 
         height="100%" 
         width="100%" 
         onclick="document.getElementById('f5').style.display ='initial'; 
                  document.getElementById('f1').style.display ='none';  
                  document.getElementById('f2').style.display ='none'; 
                  document.getElementById('f3').style.display ='none'; 
                  document.getElementById('f4').style.display ='none';
                  document.getElementById('btn').setAttribute('id', 'press');">
</button>

How can I do the same thing with pure CSS?

  • 3

    Easier to say what you want to do instead of just putting the code. This code you posted is not supposed to work anyway, because it is with errors. Suggested reading to better elaborate the question: [Ask] and MCVE. Maybe before these two links it is worth reading the [tour].

  • Therefore, it is through the button to change the displayed contents... It is working correctly, but it is not compatible with the desired browsers. Basically the code, selects an id of the range (F1 F2 F3 F4 F5) object of the respective id to be shown and hides the others. Where exactly is the error?

  • 2

    one of the problems is you use the same ID on all buttons.

  • I advise to review all your code because the way you are programming is totally illogical. 1º each button of yours should execute a function and not a block of scripts. 2º if you have no more than 1 element with the same id. 3rd functions should be applied to buttons and not to images... and so on

2 answers

7

HTML + CSS solution

Follow a CSS solution to show only one element at a time. You can test right here:

#cards input {
  display:none;                  /*  Vamos esconder os radiobuttons       */
  position:absolute;             /*  e tirar da tela pra nao aparecer     */
  left:-9000px;                  /*  marca de seleção de texto com mouse  */
}

#cards div {
  display:none;                  /*   As divs sao escondidas por padrao   */
  background:#fc9;
  float:left;
  width:50px;
  height:50px;
}

#cards input:checked + div {     /* Se existir um input selecionado a div */
  display:block;                 /* imediatamente seguinte (A+B) aparece  */
}

#buttons {
  clear:both;
}

#buttons label {
  display:block;
  float:left;
  margin:5px;
  padding:2px 20px;
  background:#ccc;
  border:1px inset #eee;
}
<div id="cards">
  <input id="c1" type="radio" name="select" checked="checked"></input><div>1</div>
  <input id="c2" type="radio" name="select"></input><div>2</div>
  <input id="c3" type="radio" name="select"></input><div>3</div>
  <input id="c4" type="radio" name="select"></input><div>4</div>
  <input id="c5" type="radio" name="select"></input><div>5</div>
</div>

<div id="buttons">
  <label for="c1">1</label>      <!--  O "label for" é uma espécie de    -->
  <label for="c2">2</label>      <!--  "controle remoto" do radiobutton  -->
  <label for="c3">3</label>
  <label for="c4">4</label>
  <label for="c5">5</label>
</div>

Of course part of the CSS above is just to "decorate" the example. Important parts are annotated with <!-- /* comments */ -->.


Javascript solution

Using a small function, you can make the code less repetitive:

function showDiv( cDiv ) {
  for(var i=1;i<=5;i++) document.getElementById('f'+i).className = ('f'+i)==cDiv?'show':'';
}
#cards div {display:none}
#cards div.show {display:block}
<div id="cards">
  <div id="f1" class="show">1</div>
  <div id="f2">2</div>
  <div id="f3">3</div>
  <div id="f4">4</div>
  <div id="f5">5</div>
</div>

<button onclick="showDiv('f1');">[imagem]</button>
<button onclick="showDiv('f2');">[imagem]</button>
<button onclick="showDiv('f3');">[imagem]</button>
<button onclick="showDiv('f4');">[imagem]</button>
<button onclick="showDiv('f5');">[imagem]</button>

  • 1

    Someone had left a comment for me yesterday about using the css+checkbox solution, but by clicking on the notification, it had already been deleted and I didn’t see who it was. The suggestion was pertinent. The only thing that changed was the radiobutton, to have only one item selected at a time.

3

I believe the best way is to do it with Javascript or using the library jQuery.

Follow an example:

$(document).ready(function() {
  $('.show-hide').on('click', function() {
    var id = this.id;

    $('.divs').hide();
    $('#f' + id).show();
  })
});
.divs {
  display: none;
}
div#f1 {
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<button class="show-hide" id="1">DIV 1</button>
<button class="show-hide" id="2">DIV 2</button>
<button class="show-hide" id="3">DIV 3</button>
<button class="show-hide" id="4">DIV 4</button>

<br/>
<br/>
<div class="divs" id="f1">DIV 1</div>
<div class="divs" id="f2">DIV 2</div>
<div class="divs" id="f3">DIV 3</div>
<div class="divs" id="f4">DIV 4</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.