I am trying to write a JS script that will change the text values of inputs' placeholder attributes when the screen size hits a width of <= 400px, so that the text is shortened rather than cut-off on a smaller screen. I've looked around the site here and for other resources that can show me how to use media queries in JS, but so far, none have worked-- Either no change is seen at any width, or the shorter text is applied regardless of whether or not it is at/under 400. The code I have currently is shown below; it is one of the versions that shortens the text regardless of the width of the screen:
<script>
if(document.documentElement.clientWidth < 400){
$("[placeholder='State/Province']").attr('placeholder', 'State/Prov.');
$("[placeholder='Vehicle Series']").attr('placeholder', 'Series');
$("[placeholder='Engine Size']").attr('placeholder', 'Eng. Size');
$("[placeholder='Transmission Type']").attr('placeholder', 'Trans.');
}
</script>
Other variations I have tried to use as the "if" conditional are...
if(screen.width < 400px){...}
if(window.matchMedia("(min-width: 400px)")){...}
Other suggestions mentioned using event handlers and other methods, but if a media query-like solution is available, that's what I'd prefer to use. Either vanilla Javascript or jQuery will work. Any ideas and constructive advice will be appreciated-- Thank you.