Last active
March 14, 2019 17:13
-
-
Save davidroyer/43ce349ada175b7e8624629aee2b1384 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var formPlaceholder = document.createElement("div"); | |
<div id="tlh-form-placeholder" class="mobile-placeholder"></div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var tlhForm = document.querySelector("#tlh-form"); | |
var desktopFormParent = tlhForm.parentNode; | |
var mobileFormParent = document.getElementById("requestInfo") | |
var formPlaceholder = document.createElement("div"); | |
// tlhForm.parentNode.removeChild(tlhForm); | |
formPlaceholder.setAttribute('id', 'tlh-form-placeholder'); | |
function handleSmallerScreens() { | |
desktopFormParent.replaceChild(formPlaceholder, tlhForm) | |
mobileFormParent.replaceChild(tlhForm, formPlaceholder) | |
} | |
function handleLargerScreens() { | |
mobileFormParent.replaceChild(formPlaceholder, tlhForm) | |
desktopFormParent.replaceChild(tlhForm,formPlaceholder); | |
} | |
/* JavaScript Media Queries */ | |
if (matchMedia) { | |
const mq = window.matchMedia("(min-width: 500px)"); | |
mq.addListener(WidthChange); | |
WidthChange(mq); | |
} | |
// media query change | |
function WidthChange(mq) { | |
if (!mq.matches) { | |
// On smaller screen size | |
desktopFormParent.insertAdjacentElement("afterbegin", formPlaceholder) | |
handleSmallerScreens() | |
} else { | |
console.log("move form to DESKTOP location"); | |
mobileFormParent.insertAdjacentElement("afterbegin", formPlaceholder) | |
handleLargerScreens() | |
} | |
} | |
/* STEPS | |
- Declare variables for all elements that will be removed and added | |
- Remove desktop form | |
- Use replaceChild method to swap the form with the placeholder div | |
with the form that was just removed | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment