質問編集履歴
2
内容の修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,92 +1,56 @@
|
|
1
|
-
|
1
|
+
チェックボックスにチェックを入れた時に、label属性の背景色を変更したいです。
|
2
|
+
現在、以下のような形で実装を試みています。
|
2
3
|
|
3
|
-
|
4
|
+
※label内のチェックボックスは複数実装しています。
|
4
|
-
|
5
|
-
```
|
5
|
+
```PHP
|
6
|
+
<div id="modal-content">
|
7
|
+
<input type="text" id="keyword" name="keyword" size="30" disabled="disabled"/>
|
6
|
-
<div id="material_select">
|
8
|
+
<div id="material_select">
|
7
9
|
<div class="all-container">
|
8
10
|
<div id="check-label-container">
|
9
11
|
<label class="check-label">
|
10
12
|
<span>テスト1</span>
|
11
|
-
<input type="checkbox" name="check"
|
13
|
+
<input type="checkbox" name="check" value="テスト1">
|
12
14
|
<span class="checkmark"></span>
|
13
15
|
</label>
|
14
16
|
</div>
|
15
17
|
</div>
|
18
|
+
</div>
|
16
19
|
</div>
|
17
20
|
```
|
18
21
|
|
19
|
-
```
|
22
|
+
```JavaScript
|
23
|
+
<script>
|
20
|
-
.
|
24
|
+
window.onload = function() {
|
25
|
+
var buttons = document.getElementById('material_select').getElementsByTagName('input');
|
26
|
+
console.log('確認');
|
27
|
+
for ( var i = 0; i < buttons.length; i ++ ) {
|
28
|
+
console.log(buttons[i]);
|
29
|
+
buttons[i].onclick = function() {
|
30
|
+
console.log('確認2');
|
31
|
+
var keyword = document.getElementById('keyword');
|
32
|
+
console.log('確認3');
|
33
|
+
if(this.checked) {
|
34
|
+
keyword.value += this.value + " ";
|
35
|
+
console.log('確認4');
|
36
|
+
buttons[i].parentNode.style.backgroundColor = '#CC28A8';
|
21
|
-
|
37
|
+
} else {
|
38
|
+
keyword.value = keyword.value.replace(new RegExp(this.value + " ","g"),"");
|
39
|
+
}
|
40
|
+
}
|
41
|
+
}
|
22
42
|
}
|
43
|
+
</script>
|
44
|
+
```
|
23
45
|
|
24
|
-
/* Customize the label (the container) */
|
25
|
-
.check-label {
|
26
|
-
display: flex;
|
27
|
-
justify-content: space-between;
|
28
|
-
align-items: center;
|
29
|
-
cursor: pointer;
|
30
|
-
font-size: 1.5rem;
|
31
|
-
user-select: none;
|
32
|
-
border-bottom: 1px solid #a5a3a3;
|
33
|
-
margin: 10px 0 10px;
|
34
|
-
}
|
35
46
|
|
36
|
-
/* Hide the browser's default checkbox */
|
37
|
-
.check-label input {
|
38
|
-
position: absolute;
|
39
|
-
opacity: 0;
|
40
|
-
cursor: pointer;
|
41
|
-
}
|
42
|
-
|
43
|
-
|
47
|
+
コンソールでは確認4まで出力された後に、以下のメッセージが出力されています。
|
44
|
-
.checkmark {
|
45
|
-
height: 25px;
|
46
|
-
width: 25px;
|
47
|
-
}
|
48
|
-
|
49
|
-
/* On mouse-over, add a grey background color */
|
50
|
-
.check-label:hover {
|
51
|
-
background-color: #dcdcdc;
|
52
|
-
}
|
53
|
-
|
54
|
-
/* Create the checkmark/indicator (hidden when not checked) */
|
55
|
-
.checkmark:after {
|
56
|
-
content: "";
|
57
|
-
position: absolute;
|
58
|
-
display: none;
|
59
|
-
}
|
60
|
-
|
61
|
-
/* Show the checkmark when checked */
|
62
|
-
.check-label input:checked ~ .checkmark:after {
|
63
|
-
display: block;
|
64
|
-
}
|
65
|
-
|
66
|
-
/* Style the checkmark/indicator */
|
67
|
-
.check-label .checkmark:after {
|
68
|
-
position: relative;
|
69
|
-
left: 9px;
|
70
|
-
top: 5px;
|
71
|
-
width: 5px;
|
72
|
-
height: 10px;
|
73
|
-
border: solid #2196F3;
|
74
|
-
border-width: 0 3px 3px 0;
|
75
|
-
transform: rotate(45deg);
|
76
|
-
}
|
77
48
|
```
|
49
|
+
Uncaught TypeError: Cannot read property 'parentNode' of undefined
|
50
|
+
at HTMLInputElement.buttons.(/anonymous function).onclick
|
51
|
+
```
|
78
52
|
|
79
|
-
|
53
|
+
getElementsByTagNameの戻り値がリストなのでparentNodeでは親要素を取得できていないのかなというところまでは何となく分かったのですが、どのように対処すれば良いのかがいまいち分かりません...
|
80
|
-
以下に書いてみたJavaScriptを載せますが、どこが間違っていますでしょうか。
|
81
54
|
|
82
|
-
```JavaScript
|
83
|
-
function ChangeColor(){
|
84
|
-
Myid=document.getElementById("material_select").getElementsByTagName('input');
|
85
|
-
if(Myid.checked == true){
|
86
|
-
Myid.parentNode.style.backgroundColor = '#CC28A8';
|
87
|
-
Myid.parentNode.style.color = '#FFFFFF';
|
88
|
-
}
|
89
|
-
}
|
90
|
-
```
|
91
55
|
|
92
|
-
|
56
|
+
どなたかご教授お願い致します。
|
1
情報追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -8,7 +8,7 @@
|
|
8
8
|
<div id="check-label-container">
|
9
9
|
<label class="check-label">
|
10
10
|
<span>テスト1</span>
|
11
|
-
<input type="checkbox" name="check" value="テスト1">
|
11
|
+
<input type="checkbox" name="check" onclick="javaScript:ChangeColor()" value="テスト1">
|
12
12
|
<span class="checkmark"></span>
|
13
13
|
</label>
|
14
14
|
</div>
|