Using media queries in JS

Hi. I’m trying to use the mediaquerylist object in a program and would like to not be hard-coding values in window.matchmedia(); Has anyone ever tried to do something like this?

window.matchMedia("(max-width:" + somefunction + “px )”);

Mine doesn’t seem to be working and I’m not sure if it’s because matchMedia only accepts actual CSS arguments.

Thanks in advance!

If you are passing in a function, replace somefunction with somefunction() to ensure the value evaluates. Or, is that what you are doing and just omitted the ( and ) characters from the example? :slight_smile:

Kirupa that did the trick! Sometimes I wish I could just reach into this screen and hug you. Btw, I just received your new book Canvas. It’s likely I won’t pick it up for a little bit, but can’t wait to dig in.

1 Like

haha, that is great that was the solution! I would have been out of ideas otherwise :stuck_out_tongue:

Almost had you stumped. Can’t believe it… You should see this code I’m writing. I know you’re busy, but I can’t believe I’m creating something that is a work around to the limitations of the window.matchMedia() object.

1 Like