After researching examples and tutorials on HTML/CSS/jQuery, I was able to create a set of buttons that toggle content based on user interaction. However, I believe there must be a more efficient method of writing the code by using element identifiers or a similar approach. Since I am new to jQuery, I am open to any suggestions for improvement.
I am specifically seeking a solution that utilizes jQuery and Bootstrap.
$(document).ready(function() {
$("#button1").click(function() {
$("#div1").show();
$("#div2").hide();
$("#div3").hide();
$("#div4").hide();
});
$("#button2").click(function() {
$("#div1").hide();
$("#div2").show();
$("#div3").hide();
$("#div4").hide();
});
$("#button3").click(function() {
$("#div1").hide();
$("#div2").hide();
$("#div3").show();
$("#div4").hide();
});
$("#button4").click(function() {
$("#div1").hide();
$("#div2").hide();
$("#div3").hide();
$("#div4").show();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="container">
<h2>Button group</h2>
<div class="btn-group">
<button id="button1" type="button" class="btn btn-primary">Div 1</button>
<button id="button2" type="button" class="btn btn-primary">Div 2</button>
<button id="button3" type="button" class="btn btn-primary">Div 3</button>
<button id="button4" type="button" class="btn btn-primary">Div 4</button>
</div>
<div class="div-group">
<div id="div1" style="background-color: red;">div 1</div>
<div id="div2" style="background-color: blue;">div 2</div>
<div id="div3" style="background-color: red;">div 3</div>
<div id="div4" style="background-color: blue;">div 4</div>
</div>
</div>