I am working with a Bootstrap 3 template and built a little JavaScript app. I am wanting to add a certain amount of margin to an element in one of the phases I created based on how big the viewport is. I have some code that I'm trying to work with but I keep getting the message Uncaught TypeError: Cannot read property 'matches' of undefined at MediaQueryList.myFunction. I cannot figure out why because all my variables that I am passing into the function as a parameter have values. I can see that the first value works because 17em is being applied but the value isn't changing when the size of the window changes. Disclaimer: I am fairly new to Javascript.
var x = window.matchMedia("screen and (min-width: 1200px)"),
y = window.matchMedia("screen and (min-width: 992px)"),
z = window.matchMedia("screen and (min-width: 768px)");
function myFunction(x, y, z) {
if (x.matches && phase === phases.crustType) {
doughSpan.style.marginLeft = "17em";
} else if (y.matches && phase === phases.crustType) {
doughSpan.style.marginLeft = "13em";
} else if (z.matches && phase === phases.crustType) {
doughSpan.style.marginLeft = "12em";
} else {
doughSpan.style.marginLeft = "0em";
}
}
myFunction(x, y, z);
x.addListener(myFunction);
y.addListener(myFunction);
z.addListener(myFunction);