The first code you added is just a once only code because you missed out the event listener. It would only add the new element if you were already at the smaller width and only on load.
The adjusted snippet I gave you above added the event listener so the element is added when the media query breakpoint is activated.
However you have not yet added the code to remove the element so all you get is an element added each time you return to the smaller screen
I gave you a working demo in the codepen showing the how to add and also remove the element. I assumed you had left it out on purpose just for testing
There is one flaw here. You pass in mqls to mediaqueryresponse on the initial call which is good.
mediaqueryresponse(mqls);
However you then donāt use it (Note mql and mqls)
function mediaqueryresponse(mql) {
if (mqls.matches) {
Instead referring to the mqls outside of the function. Changing your function parameter from mql to mqls should fix that. It then keeps matches nicely contained inside of the function,
You could (if you like) also make use of an early return to get rid of the need for that else statement. Note I have gone for an easier to read and remember āmediaQueryListā parameter here
function mediaqueryresponse(mediaQueryList) {
if (mediaQueryList.matches) {
const contactInfo = document.querySelector('.phoneNumberBox')
if (contactInfo !== null) contactInfo.remove()
return // exit here
}
// we didn't exit? Not a match then!
hostElement.insertAdjacentHTML("afterend", parasiteElement)
}
Note: I havenāt tested this. Just a look at your code.
@bendqh1 you will need to open the console at the bottom. Note how many times the resize event fires compared to matchmedia. matchmedia is more efficient and frees up the event loop to handle other things.
I checked for such event notifications on both Google Chrome console and Microsoft Edge console (in the end) but didnāt find any; I only find warnings and/or errors there.
Anyway, I understand that idea about software performance.
But isnāt something like mqls.addEventListener("change", mediaqueryresponse); on each webpage in a large website also a problem?
The problem I see (with my limited js knowledge) is that you donāt get the change at exactly the right point and it makes it feel laggy compared to the matchMedia version.
e.g.
You can see the red appears exactly when it should with the matchMedia routine but the debounced resize (blue background) you have to wait until the debounce has finished. It makes it look laggy just for a color change but imagine if this was a layout change and then the layout could possibly be broken until the debounce has finished because the screen may have been resized quite a bit smaller during that time.
I doubt there is any noticeable impact compared to using the resize event which is known resource hog.
I would go with matchMedia, hands down. In fact I will be amending a responsive menu I wrote previously with matchMedia.
I was just giving an example. A better use for debounce would be say for instance a search box, where you donāt want the database being thrashed on every key press, but rather on the completion of typing a word.