Reset DOM to original state after programmatic manipulation


I have made a function which does some manipulation to DOM, it’s going to be tied to if statement with a breakPoint.matches.

when the breakpoint gets triggered once again, for example when going back to desktop resolution, I need all DOM elements on their initial locations.

What’s the best way to do this?

function repositionHighwaySelectorForMobile() { 
  if (breakPoint.matches) {
  const highwayPicSelector = document.querySelector(
  const mobileTitleAndSelectorWrapper = document.createElement("div");
  const highwaySection = document.querySelector(".highway");
  const highwayTitle = document.querySelector(".highway__title");
  const highwayText = document.querySelector(".highway__text");
  const highwayArrowLeft = document.querySelector(".highway__arrow-left");
  const highwayArrowRight = document.querySelector(".highway__arrow-right");
  const highwayArrowLeftContainer = document.querySelector(
  const highwayArrowRightContainer = document.querySelector(

  mobileTitleAndSelectorWrapper.appendChild(highwayPicSelector); = "flex"; = "row"; = "space-between"; = "center"; = "1px solid red";

  highwayArrowLeft.src = "./images/small-arrow-left.svg";
  highwayArrowRight.src = "./images/small-arrow-right.svg"; = "24px"; = "24px"; = "24px"; = "24px";

  } else {

First off would be to use CSS for the styling instead of JavaScript… Is there a reason you’re using JS for this?

To answer your question, do what you’re doing but the opposite.

@colinthornton the task includes changing positions of HTML elements, I don’t know how to do it with css.

I understand that I could just code everything with opposite methods, but perhaps there is a more elegant solution?

Do you have a link to the instructions for your task? Are you only allowed to use JS or can you change the CSS and HTML as well?

If you’re using flex or grid, then you can change the placement of elements via order or grid-area. If you can change the HTML then you can have both the mobile and desktop elements in the DOM at all times, and swap between display: block; and display: none; via media queries.

(I don’t think there’s an elegant solution if you can only use JS)

assign a “css” class to it, and then in "if … else " block simply “add/remove” it

hope that helps, happy learning :slight_smile:

@bappyasif thanks for your reply, but how do I assign a class to a bunch of DOM manipulations? please, explain.

its pretty similar to what you are doing here, except you would use a different method (classList/className)to assign a “css class” to that element

try reading along this w3school hands on guide for this, happy reading :slight_smile: How To Add a Class Name

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.