1

I'd like to create a program where in: * When you click Span 1, Span 2 will be displayed and Span 1 will be disabled. * When you click Span 2, Span 1 will be displayed and Span 2 will be disabled.

This is my codes and I think it's strange. Your help is greatly appreciated.

CSS

body {
  display: block;
}
.span1:focus ~ .span2 {
  display: none;
}
.span2:focus ~ .span1 {
  display: block;
}

HTML

<span class="span1" tabindex="0">Span 1</span>
<span class="span2" tabindex="0">Span 2</span>
acknolodgia
  • 217
  • 1
  • 4
  • 16

3 Answers3

0

Use this css below:

body {
    display: block;
}
.span1:focus{
    color:gray;
}
.span2{
  color:gray;
}
.span1:focus ~ .span2 {
    display: inline-block;
    color:black;
}
.span2:focus{
    color:gray; 
}
.span2:focus ~ .span1 {
    color:black
}

Check the jsfiddle demo

Sarower Jahan
  • 1,445
  • 1
  • 13
  • 20
0

This problem can not be solved by Pure CSS and HTML , the answer can be found by javascript.

<span class="span1" tabindex="0" onclick="span1Clciked()">Span 1</span>
<span class="span2" tabindex="0" onclick="span2Clicked()">Span 2</span>

and on javascript

<script>
function span1Clicked(){
    var x = document.getElementsByClassName("span2").item(0);
    x.style.visibility='hidden';
 // other stuff

}
function span2Clicked(){
    var x = document.getElementsByClassName("span1").item(0);
    x.style.visibility='hidden';
 // other stuff

}    
</script>
0

Could be accomplished easily and with more flexibility in JavaScript. Example in jQuery:

$('span').click(function() {
  $(this).css('visibility','hidden').siblings().css('visibility','visible');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<span class="span1" tabindex="0">Span 1</span>
<span class="span2" tabindex="0">Span 2</span>
RPL
  • 3,500
  • 2
  • 21
  • 28