1. Description
1$(":enabled")처럼 사용하기 보다는 다른 Selector와 조합해서 사용하는 것이 추천된다.
(※ 이 내용은 $(":disabled")와 같으므로 여기를 참고하기 바랍니다).
:enabled는 disabled프로퍼티가 false인 것을 찾는 반면 :not([disabled])는 disabled속성이 설정되지 않은 요소들을 찾는다.
:enabled는 disabled속성을 지원하는 <button>, <input>, <optgroup>, <option>, <select>, <textarea> Element를 찾을 때 사용해야 한다.
2. Example
#1버튼을 클릭하면 checkbox가 enabled인 것을 찾아서 체크표시를 넣는다. 버튼을 클릭할 때마다 해당 스타일이 토글된다.
User Name :
User Id :
User Hobby:
Sport Movie Game Book
User Id :
User Hobby:
Sport Movie Game Book
[HTML Code]
[//HTML Code]
3. Code
더보기
<!-- ---------------------------------------------------------
--
-- Author: 1q74.tistory.com
--
--------------------------------------------------------- -->
<!doctype html>
<html lang="ko">
<head>
<meta charset="utf-8">
<title>[1q74.tistory.com] javascript/jquery/selector/enabled</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
</head>
<body>
<div name="_1q74-example-root">
<div name="_1q74-example-1">
User Name : <input type="text" name="name" value="This is user name"/>
<br/>
User Id : <input type="text" name="id" value="This is user id"/>
<br/>
User Hobby: <br/>
<input type="checkbox" name="hobby" value="Sport"/> Sport
<input type="checkbox" name="hobby" value="Movie" disabled/> Movie
<input type="checkbox" name="hobby" value="Game"/> Game
<input type="checkbox" name="hobby" value="Book" disabled/> Book
</div>
<div name="_1q74-example-bottom">
<br/>
<button name="btn-find">#1. $("input:checkbox:enabled")</button>
</div>
</div>
<hr/>
<h5>[HTML Code]</h5>
<div name="_1q74-source">
</div>
<h5>[//HTML Code]</h5>
<script>
const btnFind = $("[name='btn-find']");
const enabledCheckboxes = $("input:checkbox:enabled");
btnFind.click(function() {
enabledCheckboxes.each(function(index, el) {
const element = $(el);
const isChecked = element.is(":checked");
if(!isChecked) {
element
.prop({ checked: true })
.css({
"accent-color": "chartreuse",
});
} else {
element.prop({ checked: false })
}
});
});
// ------------------------------------------------------
// 소스출력
// ------------------------------------------------------
var textSource = $("[name='_1q74-source']");
var exampleHtml = $("[name='_1q74-example-root']").html();
var specialCharLines = textSource.text(exampleHtml).html().split("\n");
textSource.html(specialCharLines.join("<br/>").replaceAll("\t", " "));
</script>
<style>
[name|="_1q74-example"] button
,[name="_1q74-source"] button {
background: lightgray;
border: 5px outset;
min-height: 30px;
}
[name|="_1q74-example"] button:active
,[name="_1q74-source"] button:active {
border: 2px inset;
min-height: 30px;
}
</style>
</body>
</html>
4. File
[javascript][jquery][selector]enabled.html
0.00MB
5. Reference
https://api.jquery.com/enabled-selector/
'JavaScript > jQuery' 카테고리의 다른 글
【jQuery/Selector #21】:eq() Selector (0) | 2023.02.23 |
---|---|
【jQuery/Selector #20】:empty Selector (0) | 2023.02.23 |
【jQuery/Selector #18】Element Selector (“element”) (0) | 2023.02.22 |
【jQuery/Selector #17】:disabled Selector (0) | 2023.02.22 |
【jQuery/Selector #16】Descendant Selector (“ancestor descendant”) (0) | 2023.02.22 |