i’m new to javascript and i’m trying to create a page where the entry titles will be on the left (the sidebar) and when clicked, the content will appear on the right (main). the problem is that the content is appearing directly below the title, not by its side.
i tried changing the content to the div it’s supposed to be in and when i click the title nothing even happens. any ideas on how to fix this?
var entries = document.querySelectorAll(".entry");
for (var i = 0; i < entries.length; i++) {
var el = entries[i];
el.addEventListener("click", function () {
var itemId = i;
var allEntries = document.querySelectorAll(".entry");
if (this.nextElementSibling.className.match("show")) {
this.nextElementSibling.classList.remove("show");
} else {
this.nextElementSibling.classList.add("show");
}
for (var j = 0; j < allEntries.length; j++) {
if (this == allEntries[j]) {
continue;
}
allEntries[j].nextElementSibling.classList.remove("show");
}
});
}
.entry {
color: #d49a95;
cursor: pointer;
padding: 10px;
}
.entry-content {
display: none;
}
.show {
display: block;
}
<div class="main">
<div class="sidenav">
<div class="entry">test</div>
<div class="entry">test2</div>
</div>
<main>
<center>
<p class="entry-content">helloo</p>
<p class="entry-content">hiiii</p>
</center>
</main>
</div>
2
Answers
The issue is because you’re using
nextElementSibling()
from the clicked.entry
elements, yet they have no sibling. From the description of the issue it looks like you’re trying to access the related.entry-content
elements, however they are children of a sibling to the parent of the.entry
.To simplify the issue you could relate them by index within their respective parent elements, like this:
Or alternatively you could use
data
attributes on the.entry
elements to directly specify which.entry-content
should be displayed:Also, as an aside, using things like
cursor: pointer
to make a non-clickable element behave like a clickable element is very bad practice. I would strongly suggest you change the.entry
elements to<a />
, callingpreventDefault()
if you don’t want the links to actually go anywhere.HTML:
Since I’ve changed the JavaScript entirely, I added the
onclick="showWindow(n)"
attribute to the.entry
navigation buttons. And on the.entry-content
divs, I’ve addeddata-window:n
attributes. Now the good thing about this is, you can add as many nav items and content windows as you want. What you have to do is just.entry
) and add theshowWindow(n)
attribute as well but replace n with a number, e.g. 4 since it has not been used before..entry-content
), and give it thedata-window:"n"
attribute, wheren
is same as then
in the navigation button, so 4 in this case.Now when you click the navigation button with the
showWindow(4)
attribute, it’ll show the content window withdata-window="4"
attribute. Also added a close button.CSS:
Declared styles for the close button. Used flex-box on
.main
to display content window next to the navigation bar.flex: 2;
property on main so it takes up all the available space.Javascript:
So I’ve made a lot of changes here. Honestly, a completely different approach to get the desired result. The code you attached, returns errors when ran. Hence, I’ve rewritten the entire JavaScript code.
On clicking the cross button,
.active
class is removed from all elements.When called, The element with
data-attribute=n
is stored in thewindow
variable. Then a loop runs which removes.active
class from all content windows..active
class is then added to.cross
and the window stored inwindow
variable.