LinkedNestBlog

[jQuery] .attr('checked')과 .prop('checked') 비교

by jinwon lee

JQuery .attr('checked')과 .prop('checked') 비교


ref.] http://http://api.jquery.com/prop/

 

JQuery 1.6 이하 버전에서 체크박스의 체크 여부를 알아내기 위해 사용했던 .attr("checked")를 사용하면서

JQuery 버전을 업데이트했는데 기존처럼 동작하지 못하는 일이 발생했다.(boolean type의 데이터로 결과를 return하지 못하는 것이다.)

사이트에 들어가서 조회를 하다보니 아래와 같은 내용이 있었다.  

 

elem.checked true (Boolean) Will change with checkbox state
$(elem).prop("checked") true (Boolean) Will change with checkbox state
elem.getAttribute("checked") "checked" (String) Initial state of the checkbox; does not change
$(elem).attr("checked")(1.6) "checked" (String) Initial state of the checkbox; does not change
$(elem).attr("checked")(1.6.1+) "checked" (String) Will change with checkbox state
$(elem).attr("checked")(pre-1.6) true (Boolean) Changed with checkbox state