티스토리 뷰

🌈 JavaScript

CheckBox 체크 상태 확인하기

James Wetzel 2022. 11. 14. 10:01
let isChecked = $('#takenBefore').prop('checked');
console.log(isChecked); // true (if checked at the time)

 

is() vs prop()?

So, what's the difference between is() and prop() in this context?

Both methods appear to work in much the same way from our perspective. So, what's the difference?

is() has a bit more overhead processing and parsing than prop(). Even considering the fact that prop() doesn't just access the property and does some validation, it's more performant when it comes to processing a large number of inputs, say, in a loop.

On the other hand - is() always returns a boolean value, whereas prop() just returns the underlying property's type. You can't guarantee that something else snuck in, and your code may fail at runtime if you don't use prop() properly. is() also semantically indicates the return value - a boolean, which makes it a more readable option.

 

 

참조 사이트 : https://stackabuse.com/how-to-check-if-checkbox-is-checked-in-jquery-and-javascript/

반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함