After devising 2 buttons, each intended to toggle a heading or paragraph, I encountered a slight issue. When using the 'Toggle Para' button, the switch occurs between 2 paragraphs upon 2 clicks. However, on the third click, I desire for both paragraphs to be visible, and so forth.
I find myself in need of some assistance.
function toggle() {
var x = document.getElementById("heading1");
if (x.style.display == "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
function toggle1() {
var y = document.getElementById("p1");
var z = document.getElementById("p2");
if (y.style.display == "block") {
z.style.display = "block";
y.style.display = "none";
} else if (y.style.display == "none") {
y.style.display = "block";
z.style.display = "none";
} else {
y.style.display = "none";
z.style.display = "block";
}
}
<h1 id="heading1">Toggling with Paragraphs</h1>
<script type="text/javascript" src="toggle.js"></script>
<p id="p1"> 1. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p><br>
<p id="p2"> 2. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p><br>
<input type="button" id="button1" value="Toggle H1" onclick="toggle()" />
<input type="button" id="button2" value="Toggle Para" onclick="toggle1()" />