AD JB Toolbox 01

jQuery / Reference / :button

:button은 input[type="button"]과 button 요소를 선택하는 jQuery 전용 선택자입니다. input[type="submit"]이나 input[type="reset"]까지 모든 버튼 모양의 입력칸을 선택하는 것은 아닙니다.

문법과 적용 범위

$(":button");
$("#form").find(":button");
$("button, input[type='button']");

button 태그는 type이 submit이어도 선택됩니다. 반면 input 태그는 type이 button일 때만 선택되므로, 태그 이름과 type을 함께 봐야 합니다. :button은 표준 CSS 선택자가 아니어서 document.querySelectorAll()에 그대로 넘길 수 없습니다. 일반적인 CSS 선택자를 사용하고 싶다면 button, input[type="button"]처럼 두 선택자를 쉼표로 연결합니다.

jQuery 공식 문서의 :button 정의와 인수에서 다른 호출 형태도 확인할 수 있습니다.

전체 실행 예제

아래 코드를 UTF-8 형식의 example.html 파일로 저장한 뒤 브라우저로 여세요. 예제는 jQuery 4.0.0 일반 빌드를 외부 주소에서 불러오므로 인터넷 연결이 필요합니다. 문서가 준비된 뒤 실행하도록 $(function () { ... }) 안에 코드를 작성했습니다.

<!doctype html>
<html lang="ko">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery button-selector 예제</title>
  <style>
    body { margin: 24px; font-family: sans-serif; line-height: 1.6; }
    .marked { background: #fff0b3; border: 2px solid #835b00; }
    .active { color: #a00000; font-weight: bold; }
    button, input, select { font: inherit; }
    #output { white-space: pre-wrap; overflow-wrap: anywhere; }
  </style>
  <script src="https://code.jquery.com/jquery-4.0.0.min.js"></script>
</head>
<body>
<div id="form">
  <input id="a" type="button" value="일반 버튼">
  <button id="b" type="button">button 태그</button>
  <button id="c" type="submit">전송 button</button>
  <input id="d" type="submit" value="전송 input">
  <input id="e" type="reset" value="초기화 input">
</div>
  <p id="output" aria-live="polite"></p>
  <script>
    $(function () {
      const $buttons = $("#form").find(":button");
      $buttons.addClass("marked");
      $("#output").text($buttons.map(function () { return this.id; }).get().join(", "));
    });
  </script>
</body>
</html>

실행 결과 확인

a, b, c에 테두리와 배경이 적용되고 결과에는 a, b, c가 표시됩니다. d와 e는 input의 type이 submit과 reset이므로 선택되지 않습니다. 이 예제의 div는 실제 양식이 아니어서 버튼을 눌러도 서버로 제출하지 않습니다.

사용할 때 주의할 점

  • 실제 form 안의 button은 type을 생략하면 제출 버튼으로 작동할 수 있습니다. 스크립트용 버튼은 type="button"을 명시하세요.
  • 모든 input만 선택하려고 input:button을 쓰면 button 태그가 빠집니다. 필요한 대상이 두 태그인지 먼저 결정하세요.
  • 범위를 정하지 않은 :button은 페이지 전체의 버튼을 선택합니다. 특정 양식이나 도구 모음 안으로 범위를 좁히면 다른 버튼을 바꾸는 일을 피할 수 있습니다.

용도를 비교하려면 .addClass()로 선택한 버튼 꾸미기을 참고하세요.

같은 카테고리의 다른 글
jQuery / Reference / .addBack()

jQuery / Reference / .addBack()

탐색 직전의 집합을 다시 합치는 방법과 선택자 필터의 적용 범위를 예제로 확인합니다.

jQuery / Reference / .insertAfter()

jQuery / Reference / .insertAfter()

넣을 요소를 먼저 지정해 대상 뒤에 형제로 삽입하는 문법과 after의 차이, 안전한 텍스트 삽입을 설명합니다.

jQuery / Reference / .slideUp()

jQuery / Reference / .slideUp()

내용을 접어 숨기는 문법과 시간·easing·완료 콜백을 설명하고, 연속 클릭과 처음 상태로 되돌리는 예제를 제공합니다.

jQuery / Reference / .css()

jQuery / Reference / .css()

계산된 스타일 읽기와 인라인 스타일 설정·해제 방법을 확인하고, 단위 및 jQuery 4.0의 숫자 처리 차이를 설명합니다.

jQuery / Reference / .after()

jQuery / Reference / .after()

대상의 뒤에 형제 요소를 추가하는 방법과 기존 요소 이동, append 및 insertAfter와의 차이를 설명합니다.

jQuery / Reference / .remove()

jQuery / Reference / .remove()

선택 요소와 자손을 제거하는 방법, 조건 선택자의 범위 및 empty·detach와의 차이를 설명합니다.

jQuery / Reference / .each()

jQuery / Reference / .each()

요소별 콜백의 인덱스와 this를 확인하고, 반복 중단·화살표 함수·비동기 처리의 주의점을 설명합니다.

jQuery / Reference / .removeClass()

jQuery / Reference / .removeClass()

일부 클래스와 모든 클래스의 제거 차이를 확인하고, 남은 스타일 및 인라인 스타일의 처리 범위를 설명합니다.

jQuery / Reference / .appendTo()

jQuery / Reference / .appendTo()

삽입할 내용을 먼저 지정해 대상의 마지막 자식으로 넣는 방법과 반환 집합, 이동·복제 동작을 설명합니다.

jQuery / Reference / .insertBefore()

jQuery / Reference / .insertBefore()

넣을 요소를 먼저 지정해 대상 앞에 형제로 삽입하는 문법과 before의 차이, 이동·복제 동작을 설명합니다.