AD JB Toolbox 01

jQuery / Reference / .removeAttr()

.removeAttr()는 선택한 모든 요소에서 지정한 HTML 속성을 삭제합니다. 값을 빈 문자열로 설정하는 것과는 다르며, 여러 속성은 공백으로 구분해 한 번에 제거할 수 있습니다.

문법과 적용 범위

$("#name").removeAttr("placeholder");
$("#name").removeAttr("placeholder title");

속성 이름을 문자열로 전달하며 반환값은 원래 jQuery 객체입니다. 없는 속성을 제거해도 오류가 나지 않습니다. placeholder처럼 표시 안내에 관한 속성은 지우면 안내가 없어지지만, 실제 입력값까지 지우는 것은 아닙니다. 속성 이름 앞에 점이나 #을 붙이지 않습니다.

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

전체 실행 예제

아래 코드를 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 removeAttr 예제</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>
<label for="name">이름</label>
<input id="name" type="text" placeholder="예: 홍길동" title="이름 안내">
<button id="remove" type="button">안내 속성 제거</button>
  <p id="output" aria-live="polite"></p>
  <script>
    $(function () {
      $("#remove").on("click", function () {
        $("#name").removeAttr("placeholder title");
        $("#output").text("placeholder 존재: " + document.getElementById("name").hasAttribute("placeholder") +
          " / 현재 값: " + $("#name").val());
      });
    });
  </script>
</body>
</html>

실행 결과 확인

이름을 입력한 다음 버튼을 눌러 보세요. placeholder와 title은 없어지고 결과에 placeholder 존재: false가 표시되지만 입력한 이름은 유지됩니다. 새로고침하면 원래 두 속성이 복원됩니다.

빈 문자열과 속성 삭제 비교

$("#name").attr("placeholder", "");
console.log(document.getElementById("name").hasAttribute("placeholder")); // true
$("#name").removeAttr("placeholder");
console.log(document.getElementById("name").hasAttribute("placeholder")); // false

두 경우 모두 안내 문구는 보이지 않지만 DOM의 속성 존재 여부는 다릅니다. 속성 선택자 [placeholder]를 쓰는 CSS나 스크립트에서도 이 차이가 나타납니다.

사용할 때 주의할 점

  • disabled나 checked 같은 상태를 바꾸려는 목적이라면 .prop("disabled", false), .prop("checked", false)처럼 현재 DOM 프로퍼티를 직접 설정하세요. 속성 삭제와 현재 상태 변경을 같은 의미로 처리하지 않습니다.
  • ARIA 속성을 제거하면 보조 기술에 전달되던 의미도 사라질 수 있습니다. aria-expanded의 접힘 상태를 알리려면 삭제보다 .attr("aria-expanded", "false")가 적합합니다.
  • onclick 속성 삭제는 .on()으로 등록한 핸들러를 제거하는 기능이 아닙니다. jQuery 이벤트 핸들러는 등록 방식에 맞춰 .off()로 관리하세요.

용도를 비교하려면 .attr()로 속성 설정하기, .val()로 입력값 변경하기을 참고하세요.

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

jQuery / Reference / .attr()

HTML 속성의 읽기·설정 방법과 반환값을 확인하고, 현재 입력값·체크 상태·ARIA 속성의 처리 차이를 설명합니다.

jQuery / Reference / .toggleClass()

jQuery / Reference / .toggleClass()

클래스 반전과 불리언 조건으로 상태 지정하기를 구분하고, 버튼 상태 및 jQuery 4.0에서 사용할 문법을 설명합니다.

jQuery / Reference / .find()

jQuery / Reference / .find()

현재 요소를 제외한 자손 탐색 범위를 확인하고, children·filter·has와 용도 차이를 설명합니다.

jQuery / Reference / :contains()

jQuery / Reference / :contains()

텍스트의 부분 일치와 대소문자 구분을 예제로 확인하고, 자식 텍스트와 동적인 검색어를 처리하는 방법을 설명합니다.

jQuery / Reference / .prepend()

jQuery / Reference / .prepend()

내용을 첫 번째 자식으로 추가하는 문법을 예제로 확인하고, 기존 요소 이동과 여러 대상에 삽입할 때의 동작을 설명합니다.

jQuery / Reference / .removeClass()

jQuery / Reference / .removeClass()

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

jQuery / Reference / .insertAfter()

jQuery / Reference / .insertAfter()

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

jQuery / Reference / .remove()

jQuery / Reference / .remove()

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

jQuery / Reference / .removeAttr()

jQuery / Reference / .removeAttr()

HTML 속성 삭제와 빈 문자열 설정의 차이, 입력값 보존, 불리언·ARIA 속성의 주의점을 예제로 설명합니다.

jQuery / Reference / .slideDown()

jQuery / Reference / .slideDown()

숨겨진 내용을 펼치는 문법과 초기 표시 상태를 설명하고, 효과 큐·완료 콜백·동작 줄이기를 처리하는 예제를 제공합니다.