0

How can I show/hide the image by clicking the button, in my case PROBLEM 551, using css?

SHOW/HIDE

<body>
    <div>
        <button onclick="problem551()" >PROBLEM 551</button>
        <img src="PROBLEM551.png">
        <p id="p551"></p>
    </div>
</body>

UPDATE :

I edited the code so this is :

<head>
    <script>
        document.write("<h1>\"Benvenuto nel programma\"</h1>\n<h3>Qui imparerò ad usare JavaScript facendo i problemi di Eulero</h3>");

        function problem(){
            var img = document.getElementById('problemi');
            return img.style.display = img.style.display === 'block' ? 'none' : 'block';
        }

        function problem551(){
            problem();
            .............
        }


    </script>

</head> 

<body>
    <div>
        <button onclick="problem551()" >PROBLEM 551</button>
        <img id="problemi" src="PROBLEM551.png">
        <p id="p551"></p>
    </div>
</body>

<style>
    img {....

But I've always, when I open the program, the image shown..How can I hide it ?

UPDATE :

I found the solution :

<img id="problemi" src="PROBLEM551.png" style="display: none;">
Teshtek
  • 1,212
  • 1
  • 12
  • 20

12 Answers12

4

Only html and css solution using checkbox.

input[type=checkbox]{
display:none;
}
input[type=checkbox]:checked + img{
display:none;
}

img{display:block;}
label{
display:inline-block;
  background: gray;
  border-radius: 5px;
  padding:10px;
  /* Style your button here */
}
<body>
    <div>
        <label for="problem551">problem551</label> 
        <input type="checkbox" id="problem551">
      
        <img src="https://images.google.com/images/branding/googleg/1x/googleg_standard_color_128dp.png">
        <p id="p551"></p>
    </div>
</body>
El Danielo
  • 780
  • 1
  • 8
  • 18
3

It also can be achieved without Javascript, using a hidden checkbox:

    label.show {
    /*   make it look like a button if needed */
      border: 2px solid blue;
      display: block;
      cursor: pointer;
    }
    input.show {
      /* we don't need to display the actual checkbox */
      display: none;
    }
    input.show + * {
      /* the element after the checkbox will be hidden when the checkbox is not checked */
      visibility: hidden;
    }
    input.show:checked + * {
      /* the element will be shown when the checkbox is checked */
      visibility: visible;  
    }
    img {
      /* just a placeholder for the image */
      border: 5px solid red;
      width: 300px;
      height: 300px;
    }
    <div>
        <label for="problem551" class="show">
          problem 551<br>(click to show image)
        </label>
        <input type="checkbox" class="show" name="problem551" id="problem551">
        <img src="PROBLEM551.png">
        <p id="p551"></p>
    </div>
Teneff
  • 30,564
  • 13
  • 72
  • 103
1

Why not just use JavaScript? Give the img tag an id. And then remove it by id.

<body>
    <div>
        <button onclick="problem551()" >PROBLEM 551</button>
        <img id="problem551" src="PROBLEM551.png">
        <p id="p551"></p>
    </div>
</body>

JavaScript:

function problem551() {
   var element = document.getElementById("problem551");
   element.parentNode.removeChild(element);
};
user1289451
  • 911
  • 8
  • 22
1

Its not going to fly with a button, but a checkbox could be used to handle that. The checkbox could even be styled (via a label) like a button.

<label class="btn" for="checkbox-example">test</label>
<input id="checkbox-example" type="checkbox" />
<img src="http://placehold.it/100x100" />

with following css:

label {
  background: rgba( 0, 0, 0, 0.1);
  border: 1px solid #999;
  border-radius: 4px;
  padding: 6px 12px;
}
input[type="checkbox"] {
  display: none;
  &:checked + img {
    display: none;
  }
}

http://codepen.io/amishstripclub/pen/qazzgr

Eric N
  • 2,136
  • 13
  • 13
1

If you needed to do this without JavaScript, you could replace the button with a similarly-styled <label> attached to a hidden checkbox. Then style the element based on whether the checkbox is checked.

.button {
  background: #eee;
  border-radius: 0.5em;
  padding: 0.5em 1em;
  border: 1px solid #333;
  }
#toggle-img:checked ~ img[src="PROBLEM551.png"] {
  display: none;
  }
    <div>
        <input type="checkbox" id="toggle-img" style="display:none">
        <label onclick="problem551()" class="button" for="toggle-img">PROBLEM 551</label>
        <img src="PROBLEM551.png">
        <p id="p551"></p>
    </div>
Blazemonger
  • 90,923
  • 26
  • 142
  • 180
1

This works :Pure CSS JSFiddle

You will need to use <a> however you can style it to look like and feel just like a button

 <div>
        <a id="aa" onclick="problem551()" href="#problen551" >PROBLEM 551</a>
        <img src="PROBLEM551.png" id="problen551">
        <p id="p551">rrrr</p>
    </div>




#problen551:target{
   display:inline;
}
#problen551 {
  display:none;
}
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
O_Z
  • 1,515
  • 9
  • 11
1

You can use <input type="checkbox"> element, css :checked, :not(:checked), adjacent sibling selector + selectors, :before to toggle img display property

input {
  appearance: button;
  -webkit-appearance: button;
  -moz-appearance: button;
  width: 100px;
  height: 30px;
  display: block;
}
input:before {
  content: "Problem 551";
  position: relative;
  left: 12px;
  top: 8px;
}
input:checked + img {
  display: none;
}
input:not(:checked) + img {
  display: block;
}
<body>
  <div>
    <input type="checkbox" />
    <img id="img" src="http://placehold.it/300x150?text=551">
    <p id="p551"></p>
  </div>
</body>
guest271314
  • 1
  • 15
  • 104
  • 177
0

you need javascript for that....

i moved the img in the p tag, so that i can target it by the id #p551... another approach would be, to give the img a id

function problem551(){
  image = document.getElementById("p551");
  image.style.display = 'block';
  }
p#p551{
  display:none
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
    <div>
        <button onclick="problem551()" >PROBLEM 551</button>
        
        <p id="p551"><img src="PROBLEM551.png"></p>
    </div>
</body>

i used jquery, because it is way easier... tell me, if that is no option...

update

i changed it to pure javascript...

FalcoB
  • 1,333
  • 10
  • 25
0
<body>
    <script>
        function problem551() {
            var img = document.getElementById('img551');
            img.style.display = img.style.display === 'block' ? 'none' : 'block'; 
        }
    </script>
    <div>
        <button onclick="problem551()" >PROBLEM 551</button>
        <img src="PROBLEM551.png" id="img551">
        <p id="p551"></p>
    </div>
</body>
Eugene Tsakh
  • 2,777
  • 2
  • 14
  • 27
  • This' good solution, but how to get the image hidden in prior, and maybe then to show it? How to manipulate this code to do it? – Teshtek Oct 31 '16 at 15:46
  • You can use this code to show the image: document.getElementById('img551').style.display = 'block'; And this to hide: document.getElementById('img551').style.display = 'none'; – Eugene Tsakh Oct 31 '16 at 15:50
  • I want the image, when it borns, hidden...Anyway I have now the solution..ok this is my answer! :) – Teshtek Oct 31 '16 at 15:59
0

To set the visibility of our element using JavaScript, we can access the image's style.display property.

Make a small modification to your HTML file...

<img src="PROBLEM551.png" id="PROBLEM551">

In our JavaScript, we can declare the following function...

function problem551(){
    if(document.getElementById("PROBLEM551").style.display === "none"){
        document.getElementById("PROBLEM551").style.display = "inline"
    }else{
        document.getElementById("PROBLEM551").style.display = "none";
    }
}

In the function, if our display has been set as none, set display to inline (or a img's version of visible).

If display has not yet been set or is not already none, set it to none, which will make the element invisible.

MineAndCraft12
  • 671
  • 5
  • 15
0

var toggle  = document.getElementById("button");
var content = document.getElementById("image");

toggle.addEventListener("click", function(){
  content.style.display = (image.dataset.button ^= 1) ? "block" : "none";
}, false);
#image{
  display:none;
}
<button id="button">PROBLEM 551</button>
<div id="image"><img src="http://keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg">
</div>
Razia sultana
  • 2,168
  • 3
  • 15
  • 20
0

you have no solution in css for that. CSS stands for cascading style sheets.Its just give style to your page.It do not do function like show are / hide.

Cascading Style Sheets (CSS) is a simple mechanism for adding style (e.g., fonts, colors, spacing) to Web documents.

javascript hide/show element

Try to to do it by yourself so you can learn it.

You have to use javascript or jquery for this purpose. I will reccomend jquery. I hope you will understand.

Community
  • 1
  • 1
Awais Ahmad
  • 89
  • 1
  • 11