/    Sign up×
Community /Pin to ProfileBookmark

Changing HTML elements order with Javascript

Hi,
I have created this design https://codepen.io/raul-rogojan/pen/ExmoVgm. At first, I was gonna display none of the elements that I don’t want to show. But that would change the container height and that foud makes it look very bad. Adding a fixed height to the container will let me with a bunch of empty space which is not very good for UX.

The current way this is working is also not very UX-friendly. What I would like do to is to bring the “selected-items” to the top. Those items are in an array so I tried this …:

“`
function showMenuItems() {
for (let i = 0; i < menuItems.length; i++) {
menuItems[i].classList.remove(“selected-items”);
if (currentImg.dataset.food != menuItems[i].dataset.menu) {
menuItems[i].classList.add(“selected-items”);
menuItems.push(menuItems[0]);
}
if (currentImg.dataset.food == “bestsellers”) {
menuItems[i].classList.remove(“selected-items”);
}
}
}
“`

also,

“`
function showMenuItems() {
for (let i = 0; i < menuItems.length; i++) {
menuItems[i].classList.remove(“selected-items”);
if (currentImg.dataset.food != menuItems[i].dataset.menu) {
menuItems[i].classList.add(“selected-items”);
menuItems.push(menuItems[0]);
}
if (currentImg.dataset.food == “bestsellers”) {
menuItems.push(menuItems[0]);
}
if (currentImg.dataset.food == “bestsellers”) {
menuItems[i].classList.remove(“selected-items”);
}
}
}
“`

Both of which do not work and also after saving the browser crashes.
Do you have any ideas of what I could try?
**sorry for missing imgs**

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @codyhill spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.12,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...