Is there a way to avoid adding new lines of JS code every time I add a new image to the HTML?
$(document).ready(function() {
$('.btn').click(function() {
var bid = $(this).attr('id');
if(bid=="img1" || bid == "img2" || bid == "img3"){
document.getElementById("img1").style.display = "none";
document.getElementById("img2").style.display = "none";
document.getElementById("img3").style.display = "none";
} else {
document.getElementById("img4").style.display = "none";
document.getElementById("img5").style.display = "none";
}
document.getElementById(bid).style.display = "block";
});
});
CSS
.textures {
background: url("images/room.png") center / cover no-repeat;
position: static;
height: 600px;
width: 800px;
}
div>img {
display: none;
position: absolute;
}
[type=radio] {
position: absolute;
opacity: 0;
width: 0;
height: 0;
}
[type=radio] + img {
cursor: pointer;
height: 60px;
width: 80px
}
[type=radio]:checked + img {
outline: 2px solid #000;
}
HTML. If a radius input from the categories is clicked, an image from materials will be displayed.
<!--Room materials-->
<div class="textures">
<div class="wall">
<img id="img1" src="images/wall/blue.png" />
<img id="img2" src="images/wall/brick.png" />
</div>
<div class="floor">
<img id="img3" src="images/floor/cedro.png" />
<img id="img4" src="images/floor/cipres.png" />
</div>
</div>
<!--Categories-->
<h5>Walls</h5>
<label>
<input type="radio" class="btn" id="img1" name="test" value="small" />
<img src="images/thumbnail/wall/blue.png"/>
</label>
<label>
<input type="radio" class="btn" id="img2" name="test" value="small" />
<img src="images/thumbnail/wall/brick.png"/>
</label>
<h5>Floors</h5>
<label>
<input type="radio" class="btn" id="img3" name="test" value="small" />
<img src="images/thumbnail/floor/cedro.png"/>
</label>
<label>
<input type="radio" class="btn" id="img4" name="test" value="small" />
<img src="images/thumbnail/floor/cipres.png"/>
</label>
See an example here.
Similar to what can be achieved here.