윈도우 사이즈에 따라 셀렉트 박스 text변경

thgus·2023년 4월 10일
0

javascript

목록 보기
1/3
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>옵션 PC 모바일 교체</title>
  </head>
  <body>
    <select name="" id="sample-select">
      <option value="" selected>맨상단</option>
      <option value="">더미01</option>
      <option value="">더미02</option>
      <option value="">더미03</option>
    </select>
    <script>
      window.onload = function () {
        setTopOption("sample-select");
      };
      window.addEventListener("resize", resizeHandler);

      function resizeHandler() {
        setTopOption("sample-select");
      }

      function setTopOption(selectId) {
        var selectBox = document.getElementById(selectId);
        if (window.innerWidth <= 768) {
          selectBox.options[0].text = "Mobile";
          selectBox.options[0].value = "mobile";
        } else {
          selectBox.options[0].text = "Desktop";
          selectBox.options[0].value = "desktop";
        }
      }
    </script>
  </body>
</html>
profile
어쩌다보니IT

0개의 댓글