Toggle between two divs, reset once opened
I have two buttons called "About Me" and "Projects". I've made so that no matter which button I click, they both have their own content that shows "in the same place". (switches between divs)
I also implemented that you can toggle between show or not showing content once clicked on one of the buttons. However, my problem is that I want to be able to switch between "About Me" and "Projects" and ALWAYS show their content (when I switch between those two), and only hide their content when I click twice on the same button
I think that the problem is in my toggle functions, so I'll just paste the code here. Hopefully someone understands my problem.
function toggleAbout() {
var showAbout = document.getElementById("aboutBtn");
var hideAbout = document.getElementById("aboutDiv");
showAbout.addEventListener('click', function () {
hideAbout.classList.toggle("show");
});
}
function toggleproject() {
var showproject = document.getElementById("projectBtn");
var hideproject = document.getElementById("projectDiv");
showproject.addEventListener('click', function () {
hideproject.classList.toggle("show");
});
}
Functions calls are made in index.html
<button id="aboutmeBtn" class="btn" onclick="toggleAbout()"> About Me </button>
<button id="ProjectBtn" class="btn" onclick="toggleproject()"> Project</button>
javascript
add a comment |
I have two buttons called "About Me" and "Projects". I've made so that no matter which button I click, they both have their own content that shows "in the same place". (switches between divs)
I also implemented that you can toggle between show or not showing content once clicked on one of the buttons. However, my problem is that I want to be able to switch between "About Me" and "Projects" and ALWAYS show their content (when I switch between those two), and only hide their content when I click twice on the same button
I think that the problem is in my toggle functions, so I'll just paste the code here. Hopefully someone understands my problem.
function toggleAbout() {
var showAbout = document.getElementById("aboutBtn");
var hideAbout = document.getElementById("aboutDiv");
showAbout.addEventListener('click', function () {
hideAbout.classList.toggle("show");
});
}
function toggleproject() {
var showproject = document.getElementById("projectBtn");
var hideproject = document.getElementById("projectDiv");
showproject.addEventListener('click', function () {
hideproject.classList.toggle("show");
});
}
Functions calls are made in index.html
<button id="aboutmeBtn" class="btn" onclick="toggleAbout()"> About Me </button>
<button id="ProjectBtn" class="btn" onclick="toggleproject()"> Project</button>
javascript
Can you include where your callingtoggleAbout
andtoggleproject
from? I'm guessing your calling them when you click the buttons but I just want to make sure.
– Mike
Nov 20 at 23:15
1
Yes, I call them in my html file. I've included them in the post.
– ccuber
Nov 20 at 23:30
add a comment |
I have two buttons called "About Me" and "Projects". I've made so that no matter which button I click, they both have their own content that shows "in the same place". (switches between divs)
I also implemented that you can toggle between show or not showing content once clicked on one of the buttons. However, my problem is that I want to be able to switch between "About Me" and "Projects" and ALWAYS show their content (when I switch between those two), and only hide their content when I click twice on the same button
I think that the problem is in my toggle functions, so I'll just paste the code here. Hopefully someone understands my problem.
function toggleAbout() {
var showAbout = document.getElementById("aboutBtn");
var hideAbout = document.getElementById("aboutDiv");
showAbout.addEventListener('click', function () {
hideAbout.classList.toggle("show");
});
}
function toggleproject() {
var showproject = document.getElementById("projectBtn");
var hideproject = document.getElementById("projectDiv");
showproject.addEventListener('click', function () {
hideproject.classList.toggle("show");
});
}
Functions calls are made in index.html
<button id="aboutmeBtn" class="btn" onclick="toggleAbout()"> About Me </button>
<button id="ProjectBtn" class="btn" onclick="toggleproject()"> Project</button>
javascript
I have two buttons called "About Me" and "Projects". I've made so that no matter which button I click, they both have their own content that shows "in the same place". (switches between divs)
I also implemented that you can toggle between show or not showing content once clicked on one of the buttons. However, my problem is that I want to be able to switch between "About Me" and "Projects" and ALWAYS show their content (when I switch between those two), and only hide their content when I click twice on the same button
I think that the problem is in my toggle functions, so I'll just paste the code here. Hopefully someone understands my problem.
function toggleAbout() {
var showAbout = document.getElementById("aboutBtn");
var hideAbout = document.getElementById("aboutDiv");
showAbout.addEventListener('click', function () {
hideAbout.classList.toggle("show");
});
}
function toggleproject() {
var showproject = document.getElementById("projectBtn");
var hideproject = document.getElementById("projectDiv");
showproject.addEventListener('click', function () {
hideproject.classList.toggle("show");
});
}
Functions calls are made in index.html
<button id="aboutmeBtn" class="btn" onclick="toggleAbout()"> About Me </button>
<button id="ProjectBtn" class="btn" onclick="toggleproject()"> Project</button>
javascript
javascript
edited Nov 20 at 23:32
asked Nov 20 at 22:58
ccuber
83
83
Can you include where your callingtoggleAbout
andtoggleproject
from? I'm guessing your calling them when you click the buttons but I just want to make sure.
– Mike
Nov 20 at 23:15
1
Yes, I call them in my html file. I've included them in the post.
– ccuber
Nov 20 at 23:30
add a comment |
Can you include where your callingtoggleAbout
andtoggleproject
from? I'm guessing your calling them when you click the buttons but I just want to make sure.
– Mike
Nov 20 at 23:15
1
Yes, I call them in my html file. I've included them in the post.
– ccuber
Nov 20 at 23:30
Can you include where your calling
toggleAbout
and toggleproject
from? I'm guessing your calling them when you click the buttons but I just want to make sure.– Mike
Nov 20 at 23:15
Can you include where your calling
toggleAbout
and toggleproject
from? I'm guessing your calling them when you click the buttons but I just want to make sure.– Mike
Nov 20 at 23:15
1
1
Yes, I call them in my html file. I've included them in the post.
– ccuber
Nov 20 at 23:30
Yes, I call them in my html file. I've included them in the post.
– ccuber
Nov 20 at 23:30
add a comment |
2 Answers
2
active
oldest
votes
I think that @JO3-W3B-D3V solution is a bit complicated so I'll try and answer differently.
I think your problem is that your running the addEventListeners
when you click on the buttons but it should run when the page gets loaded instead.
Also I took out the toggle and instead changed the classes by hand if it's shown or hidden.
Try this:
var showAbout = document.getElementById("aboutBtn");
var hideAbout = document.getElementById("aboutDiv");
var showproject = document.getElementById("projectBtn");
var hideproject = document.getElementById("projectDiv");
showAbout.addEventListener('click', function () {
if (hideAbout.classList == 'hide') {
hideAbout.classList = 'show';
hideproject.classList = 'hide';
} else {
hideAbout.classList = 'hide';
hideproject.classList = 'hide';
}
});
showproject.addEventListener('click', function () {
if (hideproject.classList == 'hide') {
hideproject.classList = 'show';
hideAbout.classList = 'hide';
} else {
hideproject.classList = 'hide';
hideAbout.classList = 'hide';
}
});
.show {
display: block;
}
.hide {
display: none;
}
#yourApp div {
height: 120px;
width: 120px;
}
#aboutDiv {
background-color: pink;
}
#projectDiv {
background-color: dodgerblue;
}
#aboutBtn {
background-color: pink
}
#projectBtn {
background-color: dodgerblue
}
<div id="yourApp">
<div id='aboutDiv' class='show'>About</div>
<div id='projectDiv' class='hide'>Project</div>
<button id="aboutBtn">About</button>
<button id="projectBtn">Project</button>
</div>
I agree, but I wasn't 100% sure what he wanted... So I thought heyho, give him as much as possible!?
– JO3-W3B-D3V
Nov 20 at 23:56
1
Yeah, I'm sorry I didn't read the question properly, just editing it now
– Mike
Nov 20 at 23:58
1
Hey, no worries Mike, I mean I know my solution is a little overly engineered for the problem at hand, but at least it could scale to some extent, haha.
– JO3-W3B-D3V
Nov 21 at 0:03
1
That's exactly what I needed. Thanks!!
– ccuber
Nov 21 at 21:45
add a comment |
Is this the kinda thing you're after? ...
!function () {
var aboutBtn = document.getElementById("aboutBtn");
var projectBtn = document.getElementById("projectBtn");
var aboutPage = document.getElementById("aboutDiv");
var projectPage = document.getElementById("projectDiv");
// The class name for visible elements.
var showState = 'show';
// The class name for hidden elements.
var hideState = 'hide';
// Boolean value to state if you wish to
// automatically toggle between
// the two elements.
var toggle = false;
// Forces the other element to be hidden.
var hideOther = true;
// Simply states if the provided element
// is visible or not.
var isVisible = function (el) {
return el.className.toLowerCase().indexOf(showState) >= 0;
};
// Simple method to swap the visibility
// state.
var swapState = function (el, oel) {
if (isVisible(el)) el.className = hideState;
else el.className = showState;
if (oel != null)
if (el.className === showState) oel.className = hideState;
else oel.className = showState;
};
var controller = function (el) {
var me, other;
// Simply workout which button has been pressed.
if (el.getAttribute('id') === projectBtn.getAttribute('id')) {
me = projectPage;
other = aboutPage;
} else {
me = aboutPage;
other = projectPage;
}
// If toggle is false.
if (!toggle) swapState(me);
else swapState(me, other);
// Both wouldn't really work together,
// at least to my knowledge.
if (hideOther && !toggle) other.className = hideState;
};
// Simply bind the event handler.
aboutBtn.addEventListener('click', function () { controller(aboutBtn); });
projectBtn.addEventListener('click', function () { controller(projectBtn); });
}();
.show {
display: block;
}
.hide {
display: none;
}
#yourApp div {
height: 100px;
width: 100px;
}
#aboutDiv {
background: pink;
}
#projectDiv {
background: dodgerblue;
}
<div id="yourApp">
<div id='aboutDiv' class='show'></div>
<div id='projectDiv' class='hide'></div>
<button id="aboutBtn">About</button>
<button id="projectBtn">Project</button>
</div>
The switching between the two buttons are correct, but I also want it to be able to (in your example) to toggle between the colors when I click on the the same button, Like: 1st click - opens div 2nd click - closes div 3rd click - opens div
– ccuber
Nov 20 at 23:16
In that case you just do something like what I've done above.. Only you set the class-name property to 'hide' in the example I provided.
– JO3-W3B-D3V
Nov 20 at 23:20
I'll try that! Thanks!!
– ccuber
Nov 20 at 23:26
Hope my most recent update is as helpful as possible, I even included comments in that version.
– JO3-W3B-D3V
Nov 20 at 23:42
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402868%2ftoggle-between-two-divs-reset-once-opened%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think that @JO3-W3B-D3V solution is a bit complicated so I'll try and answer differently.
I think your problem is that your running the addEventListeners
when you click on the buttons but it should run when the page gets loaded instead.
Also I took out the toggle and instead changed the classes by hand if it's shown or hidden.
Try this:
var showAbout = document.getElementById("aboutBtn");
var hideAbout = document.getElementById("aboutDiv");
var showproject = document.getElementById("projectBtn");
var hideproject = document.getElementById("projectDiv");
showAbout.addEventListener('click', function () {
if (hideAbout.classList == 'hide') {
hideAbout.classList = 'show';
hideproject.classList = 'hide';
} else {
hideAbout.classList = 'hide';
hideproject.classList = 'hide';
}
});
showproject.addEventListener('click', function () {
if (hideproject.classList == 'hide') {
hideproject.classList = 'show';
hideAbout.classList = 'hide';
} else {
hideproject.classList = 'hide';
hideAbout.classList = 'hide';
}
});
.show {
display: block;
}
.hide {
display: none;
}
#yourApp div {
height: 120px;
width: 120px;
}
#aboutDiv {
background-color: pink;
}
#projectDiv {
background-color: dodgerblue;
}
#aboutBtn {
background-color: pink
}
#projectBtn {
background-color: dodgerblue
}
<div id="yourApp">
<div id='aboutDiv' class='show'>About</div>
<div id='projectDiv' class='hide'>Project</div>
<button id="aboutBtn">About</button>
<button id="projectBtn">Project</button>
</div>
I agree, but I wasn't 100% sure what he wanted... So I thought heyho, give him as much as possible!?
– JO3-W3B-D3V
Nov 20 at 23:56
1
Yeah, I'm sorry I didn't read the question properly, just editing it now
– Mike
Nov 20 at 23:58
1
Hey, no worries Mike, I mean I know my solution is a little overly engineered for the problem at hand, but at least it could scale to some extent, haha.
– JO3-W3B-D3V
Nov 21 at 0:03
1
That's exactly what I needed. Thanks!!
– ccuber
Nov 21 at 21:45
add a comment |
I think that @JO3-W3B-D3V solution is a bit complicated so I'll try and answer differently.
I think your problem is that your running the addEventListeners
when you click on the buttons but it should run when the page gets loaded instead.
Also I took out the toggle and instead changed the classes by hand if it's shown or hidden.
Try this:
var showAbout = document.getElementById("aboutBtn");
var hideAbout = document.getElementById("aboutDiv");
var showproject = document.getElementById("projectBtn");
var hideproject = document.getElementById("projectDiv");
showAbout.addEventListener('click', function () {
if (hideAbout.classList == 'hide') {
hideAbout.classList = 'show';
hideproject.classList = 'hide';
} else {
hideAbout.classList = 'hide';
hideproject.classList = 'hide';
}
});
showproject.addEventListener('click', function () {
if (hideproject.classList == 'hide') {
hideproject.classList = 'show';
hideAbout.classList = 'hide';
} else {
hideproject.classList = 'hide';
hideAbout.classList = 'hide';
}
});
.show {
display: block;
}
.hide {
display: none;
}
#yourApp div {
height: 120px;
width: 120px;
}
#aboutDiv {
background-color: pink;
}
#projectDiv {
background-color: dodgerblue;
}
#aboutBtn {
background-color: pink
}
#projectBtn {
background-color: dodgerblue
}
<div id="yourApp">
<div id='aboutDiv' class='show'>About</div>
<div id='projectDiv' class='hide'>Project</div>
<button id="aboutBtn">About</button>
<button id="projectBtn">Project</button>
</div>
I agree, but I wasn't 100% sure what he wanted... So I thought heyho, give him as much as possible!?
– JO3-W3B-D3V
Nov 20 at 23:56
1
Yeah, I'm sorry I didn't read the question properly, just editing it now
– Mike
Nov 20 at 23:58
1
Hey, no worries Mike, I mean I know my solution is a little overly engineered for the problem at hand, but at least it could scale to some extent, haha.
– JO3-W3B-D3V
Nov 21 at 0:03
1
That's exactly what I needed. Thanks!!
– ccuber
Nov 21 at 21:45
add a comment |
I think that @JO3-W3B-D3V solution is a bit complicated so I'll try and answer differently.
I think your problem is that your running the addEventListeners
when you click on the buttons but it should run when the page gets loaded instead.
Also I took out the toggle and instead changed the classes by hand if it's shown or hidden.
Try this:
var showAbout = document.getElementById("aboutBtn");
var hideAbout = document.getElementById("aboutDiv");
var showproject = document.getElementById("projectBtn");
var hideproject = document.getElementById("projectDiv");
showAbout.addEventListener('click', function () {
if (hideAbout.classList == 'hide') {
hideAbout.classList = 'show';
hideproject.classList = 'hide';
} else {
hideAbout.classList = 'hide';
hideproject.classList = 'hide';
}
});
showproject.addEventListener('click', function () {
if (hideproject.classList == 'hide') {
hideproject.classList = 'show';
hideAbout.classList = 'hide';
} else {
hideproject.classList = 'hide';
hideAbout.classList = 'hide';
}
});
.show {
display: block;
}
.hide {
display: none;
}
#yourApp div {
height: 120px;
width: 120px;
}
#aboutDiv {
background-color: pink;
}
#projectDiv {
background-color: dodgerblue;
}
#aboutBtn {
background-color: pink
}
#projectBtn {
background-color: dodgerblue
}
<div id="yourApp">
<div id='aboutDiv' class='show'>About</div>
<div id='projectDiv' class='hide'>Project</div>
<button id="aboutBtn">About</button>
<button id="projectBtn">Project</button>
</div>
I think that @JO3-W3B-D3V solution is a bit complicated so I'll try and answer differently.
I think your problem is that your running the addEventListeners
when you click on the buttons but it should run when the page gets loaded instead.
Also I took out the toggle and instead changed the classes by hand if it's shown or hidden.
Try this:
var showAbout = document.getElementById("aboutBtn");
var hideAbout = document.getElementById("aboutDiv");
var showproject = document.getElementById("projectBtn");
var hideproject = document.getElementById("projectDiv");
showAbout.addEventListener('click', function () {
if (hideAbout.classList == 'hide') {
hideAbout.classList = 'show';
hideproject.classList = 'hide';
} else {
hideAbout.classList = 'hide';
hideproject.classList = 'hide';
}
});
showproject.addEventListener('click', function () {
if (hideproject.classList == 'hide') {
hideproject.classList = 'show';
hideAbout.classList = 'hide';
} else {
hideproject.classList = 'hide';
hideAbout.classList = 'hide';
}
});
.show {
display: block;
}
.hide {
display: none;
}
#yourApp div {
height: 120px;
width: 120px;
}
#aboutDiv {
background-color: pink;
}
#projectDiv {
background-color: dodgerblue;
}
#aboutBtn {
background-color: pink
}
#projectBtn {
background-color: dodgerblue
}
<div id="yourApp">
<div id='aboutDiv' class='show'>About</div>
<div id='projectDiv' class='hide'>Project</div>
<button id="aboutBtn">About</button>
<button id="projectBtn">Project</button>
</div>
var showAbout = document.getElementById("aboutBtn");
var hideAbout = document.getElementById("aboutDiv");
var showproject = document.getElementById("projectBtn");
var hideproject = document.getElementById("projectDiv");
showAbout.addEventListener('click', function () {
if (hideAbout.classList == 'hide') {
hideAbout.classList = 'show';
hideproject.classList = 'hide';
} else {
hideAbout.classList = 'hide';
hideproject.classList = 'hide';
}
});
showproject.addEventListener('click', function () {
if (hideproject.classList == 'hide') {
hideproject.classList = 'show';
hideAbout.classList = 'hide';
} else {
hideproject.classList = 'hide';
hideAbout.classList = 'hide';
}
});
.show {
display: block;
}
.hide {
display: none;
}
#yourApp div {
height: 120px;
width: 120px;
}
#aboutDiv {
background-color: pink;
}
#projectDiv {
background-color: dodgerblue;
}
#aboutBtn {
background-color: pink
}
#projectBtn {
background-color: dodgerblue
}
<div id="yourApp">
<div id='aboutDiv' class='show'>About</div>
<div id='projectDiv' class='hide'>Project</div>
<button id="aboutBtn">About</button>
<button id="projectBtn">Project</button>
</div>
var showAbout = document.getElementById("aboutBtn");
var hideAbout = document.getElementById("aboutDiv");
var showproject = document.getElementById("projectBtn");
var hideproject = document.getElementById("projectDiv");
showAbout.addEventListener('click', function () {
if (hideAbout.classList == 'hide') {
hideAbout.classList = 'show';
hideproject.classList = 'hide';
} else {
hideAbout.classList = 'hide';
hideproject.classList = 'hide';
}
});
showproject.addEventListener('click', function () {
if (hideproject.classList == 'hide') {
hideproject.classList = 'show';
hideAbout.classList = 'hide';
} else {
hideproject.classList = 'hide';
hideAbout.classList = 'hide';
}
});
.show {
display: block;
}
.hide {
display: none;
}
#yourApp div {
height: 120px;
width: 120px;
}
#aboutDiv {
background-color: pink;
}
#projectDiv {
background-color: dodgerblue;
}
#aboutBtn {
background-color: pink
}
#projectBtn {
background-color: dodgerblue
}
<div id="yourApp">
<div id='aboutDiv' class='show'>About</div>
<div id='projectDiv' class='hide'>Project</div>
<button id="aboutBtn">About</button>
<button id="projectBtn">Project</button>
</div>
edited Nov 21 at 7:38
answered Nov 20 at 23:51
Mike
1,3811823
1,3811823
I agree, but I wasn't 100% sure what he wanted... So I thought heyho, give him as much as possible!?
– JO3-W3B-D3V
Nov 20 at 23:56
1
Yeah, I'm sorry I didn't read the question properly, just editing it now
– Mike
Nov 20 at 23:58
1
Hey, no worries Mike, I mean I know my solution is a little overly engineered for the problem at hand, but at least it could scale to some extent, haha.
– JO3-W3B-D3V
Nov 21 at 0:03
1
That's exactly what I needed. Thanks!!
– ccuber
Nov 21 at 21:45
add a comment |
I agree, but I wasn't 100% sure what he wanted... So I thought heyho, give him as much as possible!?
– JO3-W3B-D3V
Nov 20 at 23:56
1
Yeah, I'm sorry I didn't read the question properly, just editing it now
– Mike
Nov 20 at 23:58
1
Hey, no worries Mike, I mean I know my solution is a little overly engineered for the problem at hand, but at least it could scale to some extent, haha.
– JO3-W3B-D3V
Nov 21 at 0:03
1
That's exactly what I needed. Thanks!!
– ccuber
Nov 21 at 21:45
I agree, but I wasn't 100% sure what he wanted... So I thought heyho, give him as much as possible!?
– JO3-W3B-D3V
Nov 20 at 23:56
I agree, but I wasn't 100% sure what he wanted... So I thought heyho, give him as much as possible!?
– JO3-W3B-D3V
Nov 20 at 23:56
1
1
Yeah, I'm sorry I didn't read the question properly, just editing it now
– Mike
Nov 20 at 23:58
Yeah, I'm sorry I didn't read the question properly, just editing it now
– Mike
Nov 20 at 23:58
1
1
Hey, no worries Mike, I mean I know my solution is a little overly engineered for the problem at hand, but at least it could scale to some extent, haha.
– JO3-W3B-D3V
Nov 21 at 0:03
Hey, no worries Mike, I mean I know my solution is a little overly engineered for the problem at hand, but at least it could scale to some extent, haha.
– JO3-W3B-D3V
Nov 21 at 0:03
1
1
That's exactly what I needed. Thanks!!
– ccuber
Nov 21 at 21:45
That's exactly what I needed. Thanks!!
– ccuber
Nov 21 at 21:45
add a comment |
Is this the kinda thing you're after? ...
!function () {
var aboutBtn = document.getElementById("aboutBtn");
var projectBtn = document.getElementById("projectBtn");
var aboutPage = document.getElementById("aboutDiv");
var projectPage = document.getElementById("projectDiv");
// The class name for visible elements.
var showState = 'show';
// The class name for hidden elements.
var hideState = 'hide';
// Boolean value to state if you wish to
// automatically toggle between
// the two elements.
var toggle = false;
// Forces the other element to be hidden.
var hideOther = true;
// Simply states if the provided element
// is visible or not.
var isVisible = function (el) {
return el.className.toLowerCase().indexOf(showState) >= 0;
};
// Simple method to swap the visibility
// state.
var swapState = function (el, oel) {
if (isVisible(el)) el.className = hideState;
else el.className = showState;
if (oel != null)
if (el.className === showState) oel.className = hideState;
else oel.className = showState;
};
var controller = function (el) {
var me, other;
// Simply workout which button has been pressed.
if (el.getAttribute('id') === projectBtn.getAttribute('id')) {
me = projectPage;
other = aboutPage;
} else {
me = aboutPage;
other = projectPage;
}
// If toggle is false.
if (!toggle) swapState(me);
else swapState(me, other);
// Both wouldn't really work together,
// at least to my knowledge.
if (hideOther && !toggle) other.className = hideState;
};
// Simply bind the event handler.
aboutBtn.addEventListener('click', function () { controller(aboutBtn); });
projectBtn.addEventListener('click', function () { controller(projectBtn); });
}();
.show {
display: block;
}
.hide {
display: none;
}
#yourApp div {
height: 100px;
width: 100px;
}
#aboutDiv {
background: pink;
}
#projectDiv {
background: dodgerblue;
}
<div id="yourApp">
<div id='aboutDiv' class='show'></div>
<div id='projectDiv' class='hide'></div>
<button id="aboutBtn">About</button>
<button id="projectBtn">Project</button>
</div>
The switching between the two buttons are correct, but I also want it to be able to (in your example) to toggle between the colors when I click on the the same button, Like: 1st click - opens div 2nd click - closes div 3rd click - opens div
– ccuber
Nov 20 at 23:16
In that case you just do something like what I've done above.. Only you set the class-name property to 'hide' in the example I provided.
– JO3-W3B-D3V
Nov 20 at 23:20
I'll try that! Thanks!!
– ccuber
Nov 20 at 23:26
Hope my most recent update is as helpful as possible, I even included comments in that version.
– JO3-W3B-D3V
Nov 20 at 23:42
add a comment |
Is this the kinda thing you're after? ...
!function () {
var aboutBtn = document.getElementById("aboutBtn");
var projectBtn = document.getElementById("projectBtn");
var aboutPage = document.getElementById("aboutDiv");
var projectPage = document.getElementById("projectDiv");
// The class name for visible elements.
var showState = 'show';
// The class name for hidden elements.
var hideState = 'hide';
// Boolean value to state if you wish to
// automatically toggle between
// the two elements.
var toggle = false;
// Forces the other element to be hidden.
var hideOther = true;
// Simply states if the provided element
// is visible or not.
var isVisible = function (el) {
return el.className.toLowerCase().indexOf(showState) >= 0;
};
// Simple method to swap the visibility
// state.
var swapState = function (el, oel) {
if (isVisible(el)) el.className = hideState;
else el.className = showState;
if (oel != null)
if (el.className === showState) oel.className = hideState;
else oel.className = showState;
};
var controller = function (el) {
var me, other;
// Simply workout which button has been pressed.
if (el.getAttribute('id') === projectBtn.getAttribute('id')) {
me = projectPage;
other = aboutPage;
} else {
me = aboutPage;
other = projectPage;
}
// If toggle is false.
if (!toggle) swapState(me);
else swapState(me, other);
// Both wouldn't really work together,
// at least to my knowledge.
if (hideOther && !toggle) other.className = hideState;
};
// Simply bind the event handler.
aboutBtn.addEventListener('click', function () { controller(aboutBtn); });
projectBtn.addEventListener('click', function () { controller(projectBtn); });
}();
.show {
display: block;
}
.hide {
display: none;
}
#yourApp div {
height: 100px;
width: 100px;
}
#aboutDiv {
background: pink;
}
#projectDiv {
background: dodgerblue;
}
<div id="yourApp">
<div id='aboutDiv' class='show'></div>
<div id='projectDiv' class='hide'></div>
<button id="aboutBtn">About</button>
<button id="projectBtn">Project</button>
</div>
The switching between the two buttons are correct, but I also want it to be able to (in your example) to toggle between the colors when I click on the the same button, Like: 1st click - opens div 2nd click - closes div 3rd click - opens div
– ccuber
Nov 20 at 23:16
In that case you just do something like what I've done above.. Only you set the class-name property to 'hide' in the example I provided.
– JO3-W3B-D3V
Nov 20 at 23:20
I'll try that! Thanks!!
– ccuber
Nov 20 at 23:26
Hope my most recent update is as helpful as possible, I even included comments in that version.
– JO3-W3B-D3V
Nov 20 at 23:42
add a comment |
Is this the kinda thing you're after? ...
!function () {
var aboutBtn = document.getElementById("aboutBtn");
var projectBtn = document.getElementById("projectBtn");
var aboutPage = document.getElementById("aboutDiv");
var projectPage = document.getElementById("projectDiv");
// The class name for visible elements.
var showState = 'show';
// The class name for hidden elements.
var hideState = 'hide';
// Boolean value to state if you wish to
// automatically toggle between
// the two elements.
var toggle = false;
// Forces the other element to be hidden.
var hideOther = true;
// Simply states if the provided element
// is visible or not.
var isVisible = function (el) {
return el.className.toLowerCase().indexOf(showState) >= 0;
};
// Simple method to swap the visibility
// state.
var swapState = function (el, oel) {
if (isVisible(el)) el.className = hideState;
else el.className = showState;
if (oel != null)
if (el.className === showState) oel.className = hideState;
else oel.className = showState;
};
var controller = function (el) {
var me, other;
// Simply workout which button has been pressed.
if (el.getAttribute('id') === projectBtn.getAttribute('id')) {
me = projectPage;
other = aboutPage;
} else {
me = aboutPage;
other = projectPage;
}
// If toggle is false.
if (!toggle) swapState(me);
else swapState(me, other);
// Both wouldn't really work together,
// at least to my knowledge.
if (hideOther && !toggle) other.className = hideState;
};
// Simply bind the event handler.
aboutBtn.addEventListener('click', function () { controller(aboutBtn); });
projectBtn.addEventListener('click', function () { controller(projectBtn); });
}();
.show {
display: block;
}
.hide {
display: none;
}
#yourApp div {
height: 100px;
width: 100px;
}
#aboutDiv {
background: pink;
}
#projectDiv {
background: dodgerblue;
}
<div id="yourApp">
<div id='aboutDiv' class='show'></div>
<div id='projectDiv' class='hide'></div>
<button id="aboutBtn">About</button>
<button id="projectBtn">Project</button>
</div>
Is this the kinda thing you're after? ...
!function () {
var aboutBtn = document.getElementById("aboutBtn");
var projectBtn = document.getElementById("projectBtn");
var aboutPage = document.getElementById("aboutDiv");
var projectPage = document.getElementById("projectDiv");
// The class name for visible elements.
var showState = 'show';
// The class name for hidden elements.
var hideState = 'hide';
// Boolean value to state if you wish to
// automatically toggle between
// the two elements.
var toggle = false;
// Forces the other element to be hidden.
var hideOther = true;
// Simply states if the provided element
// is visible or not.
var isVisible = function (el) {
return el.className.toLowerCase().indexOf(showState) >= 0;
};
// Simple method to swap the visibility
// state.
var swapState = function (el, oel) {
if (isVisible(el)) el.className = hideState;
else el.className = showState;
if (oel != null)
if (el.className === showState) oel.className = hideState;
else oel.className = showState;
};
var controller = function (el) {
var me, other;
// Simply workout which button has been pressed.
if (el.getAttribute('id') === projectBtn.getAttribute('id')) {
me = projectPage;
other = aboutPage;
} else {
me = aboutPage;
other = projectPage;
}
// If toggle is false.
if (!toggle) swapState(me);
else swapState(me, other);
// Both wouldn't really work together,
// at least to my knowledge.
if (hideOther && !toggle) other.className = hideState;
};
// Simply bind the event handler.
aboutBtn.addEventListener('click', function () { controller(aboutBtn); });
projectBtn.addEventListener('click', function () { controller(projectBtn); });
}();
.show {
display: block;
}
.hide {
display: none;
}
#yourApp div {
height: 100px;
width: 100px;
}
#aboutDiv {
background: pink;
}
#projectDiv {
background: dodgerblue;
}
<div id="yourApp">
<div id='aboutDiv' class='show'></div>
<div id='projectDiv' class='hide'></div>
<button id="aboutBtn">About</button>
<button id="projectBtn">Project</button>
</div>
!function () {
var aboutBtn = document.getElementById("aboutBtn");
var projectBtn = document.getElementById("projectBtn");
var aboutPage = document.getElementById("aboutDiv");
var projectPage = document.getElementById("projectDiv");
// The class name for visible elements.
var showState = 'show';
// The class name for hidden elements.
var hideState = 'hide';
// Boolean value to state if you wish to
// automatically toggle between
// the two elements.
var toggle = false;
// Forces the other element to be hidden.
var hideOther = true;
// Simply states if the provided element
// is visible or not.
var isVisible = function (el) {
return el.className.toLowerCase().indexOf(showState) >= 0;
};
// Simple method to swap the visibility
// state.
var swapState = function (el, oel) {
if (isVisible(el)) el.className = hideState;
else el.className = showState;
if (oel != null)
if (el.className === showState) oel.className = hideState;
else oel.className = showState;
};
var controller = function (el) {
var me, other;
// Simply workout which button has been pressed.
if (el.getAttribute('id') === projectBtn.getAttribute('id')) {
me = projectPage;
other = aboutPage;
} else {
me = aboutPage;
other = projectPage;
}
// If toggle is false.
if (!toggle) swapState(me);
else swapState(me, other);
// Both wouldn't really work together,
// at least to my knowledge.
if (hideOther && !toggle) other.className = hideState;
};
// Simply bind the event handler.
aboutBtn.addEventListener('click', function () { controller(aboutBtn); });
projectBtn.addEventListener('click', function () { controller(projectBtn); });
}();
.show {
display: block;
}
.hide {
display: none;
}
#yourApp div {
height: 100px;
width: 100px;
}
#aboutDiv {
background: pink;
}
#projectDiv {
background: dodgerblue;
}
<div id="yourApp">
<div id='aboutDiv' class='show'></div>
<div id='projectDiv' class='hide'></div>
<button id="aboutBtn">About</button>
<button id="projectBtn">Project</button>
</div>
!function () {
var aboutBtn = document.getElementById("aboutBtn");
var projectBtn = document.getElementById("projectBtn");
var aboutPage = document.getElementById("aboutDiv");
var projectPage = document.getElementById("projectDiv");
// The class name for visible elements.
var showState = 'show';
// The class name for hidden elements.
var hideState = 'hide';
// Boolean value to state if you wish to
// automatically toggle between
// the two elements.
var toggle = false;
// Forces the other element to be hidden.
var hideOther = true;
// Simply states if the provided element
// is visible or not.
var isVisible = function (el) {
return el.className.toLowerCase().indexOf(showState) >= 0;
};
// Simple method to swap the visibility
// state.
var swapState = function (el, oel) {
if (isVisible(el)) el.className = hideState;
else el.className = showState;
if (oel != null)
if (el.className === showState) oel.className = hideState;
else oel.className = showState;
};
var controller = function (el) {
var me, other;
// Simply workout which button has been pressed.
if (el.getAttribute('id') === projectBtn.getAttribute('id')) {
me = projectPage;
other = aboutPage;
} else {
me = aboutPage;
other = projectPage;
}
// If toggle is false.
if (!toggle) swapState(me);
else swapState(me, other);
// Both wouldn't really work together,
// at least to my knowledge.
if (hideOther && !toggle) other.className = hideState;
};
// Simply bind the event handler.
aboutBtn.addEventListener('click', function () { controller(aboutBtn); });
projectBtn.addEventListener('click', function () { controller(projectBtn); });
}();
.show {
display: block;
}
.hide {
display: none;
}
#yourApp div {
height: 100px;
width: 100px;
}
#aboutDiv {
background: pink;
}
#projectDiv {
background: dodgerblue;
}
<div id="yourApp">
<div id='aboutDiv' class='show'></div>
<div id='projectDiv' class='hide'></div>
<button id="aboutBtn">About</button>
<button id="projectBtn">Project</button>
</div>
edited Nov 20 at 23:39
answered Nov 20 at 23:11
JO3-W3B-D3V
1
1
The switching between the two buttons are correct, but I also want it to be able to (in your example) to toggle between the colors when I click on the the same button, Like: 1st click - opens div 2nd click - closes div 3rd click - opens div
– ccuber
Nov 20 at 23:16
In that case you just do something like what I've done above.. Only you set the class-name property to 'hide' in the example I provided.
– JO3-W3B-D3V
Nov 20 at 23:20
I'll try that! Thanks!!
– ccuber
Nov 20 at 23:26
Hope my most recent update is as helpful as possible, I even included comments in that version.
– JO3-W3B-D3V
Nov 20 at 23:42
add a comment |
The switching between the two buttons are correct, but I also want it to be able to (in your example) to toggle between the colors when I click on the the same button, Like: 1st click - opens div 2nd click - closes div 3rd click - opens div
– ccuber
Nov 20 at 23:16
In that case you just do something like what I've done above.. Only you set the class-name property to 'hide' in the example I provided.
– JO3-W3B-D3V
Nov 20 at 23:20
I'll try that! Thanks!!
– ccuber
Nov 20 at 23:26
Hope my most recent update is as helpful as possible, I even included comments in that version.
– JO3-W3B-D3V
Nov 20 at 23:42
The switching between the two buttons are correct, but I also want it to be able to (in your example) to toggle between the colors when I click on the the same button, Like: 1st click - opens div 2nd click - closes div 3rd click - opens div
– ccuber
Nov 20 at 23:16
The switching between the two buttons are correct, but I also want it to be able to (in your example) to toggle between the colors when I click on the the same button, Like: 1st click - opens div 2nd click - closes div 3rd click - opens div
– ccuber
Nov 20 at 23:16
In that case you just do something like what I've done above.. Only you set the class-name property to 'hide' in the example I provided.
– JO3-W3B-D3V
Nov 20 at 23:20
In that case you just do something like what I've done above.. Only you set the class-name property to 'hide' in the example I provided.
– JO3-W3B-D3V
Nov 20 at 23:20
I'll try that! Thanks!!
– ccuber
Nov 20 at 23:26
I'll try that! Thanks!!
– ccuber
Nov 20 at 23:26
Hope my most recent update is as helpful as possible, I even included comments in that version.
– JO3-W3B-D3V
Nov 20 at 23:42
Hope my most recent update is as helpful as possible, I even included comments in that version.
– JO3-W3B-D3V
Nov 20 at 23:42
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402868%2ftoggle-between-two-divs-reset-once-opened%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you include where your calling
toggleAbout
andtoggleproject
from? I'm guessing your calling them when you click the buttons but I just want to make sure.– Mike
Nov 20 at 23:15
1
Yes, I call them in my html file. I've included them in the post.
– ccuber
Nov 20 at 23:30