質問編集履歴
2
内容の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,14 +1,18 @@
|
|
1
|
-
|
1
|
+
チェックボックスにチェックを入れた時に、label属性の背景色を変更したいです。
|
2
|
+
|
3
|
+
現在、以下のような形で実装を試みています。
|
2
4
|
|
3
5
|
|
4
6
|
|
5
|
-
|
7
|
+
※label内のチェックボックスは複数実装しています。
|
6
8
|
|
9
|
+
```PHP
|
7
10
|
|
11
|
+
<div id="modal-content">
|
8
12
|
|
9
|
-
|
13
|
+
<input type="text" id="keyword" name="keyword" size="30" disabled="disabled"/>
|
10
14
|
|
11
|
-
<div id="material_select">
|
15
|
+
<div id="material_select">
|
12
16
|
|
13
17
|
<div class="all-container">
|
14
18
|
|
@@ -18,7 +22,7 @@
|
|
18
22
|
|
19
23
|
<span>テスト1</span>
|
20
24
|
|
21
|
-
<input type="checkbox" name="check"
|
25
|
+
<input type="checkbox" name="check" value="テスト1">
|
22
26
|
|
23
27
|
<span class="checkmark"></span>
|
24
28
|
|
@@ -28,156 +32,80 @@
|
|
28
32
|
|
29
33
|
</div>
|
30
34
|
|
35
|
+
</div>
|
36
|
+
|
31
37
|
</div>
|
32
38
|
|
33
39
|
```
|
34
40
|
|
35
41
|
|
36
42
|
|
37
|
-
```c
|
43
|
+
```JavaScript
|
38
44
|
|
39
|
-
|
45
|
+
<script>
|
40
46
|
|
47
|
+
window.onload = function() {
|
48
|
+
|
49
|
+
var buttons = document.getElementById('material_select').getElementsByTagName('input');
|
50
|
+
|
51
|
+
console.log('確認');
|
52
|
+
|
53
|
+
for ( var i = 0; i < buttons.length; i ++ ) {
|
54
|
+
|
55
|
+
console.log(buttons[i]);
|
56
|
+
|
57
|
+
buttons[i].onclick = function() {
|
58
|
+
|
59
|
+
console.log('確認2');
|
60
|
+
|
61
|
+
var keyword = document.getElementById('keyword');
|
62
|
+
|
63
|
+
console.log('確認3');
|
64
|
+
|
65
|
+
if(this.checked) {
|
66
|
+
|
67
|
+
keyword.value += this.value + " ";
|
68
|
+
|
69
|
+
console.log('確認4');
|
70
|
+
|
71
|
+
buttons[i].parentNode.style.backgroundColor = '#CC28A8';
|
72
|
+
|
41
|
-
|
73
|
+
} else {
|
74
|
+
|
75
|
+
keyword.value = keyword.value.replace(new RegExp(this.value + " ","g"),"");
|
76
|
+
|
77
|
+
}
|
78
|
+
|
79
|
+
}
|
80
|
+
|
81
|
+
}
|
42
82
|
|
43
83
|
}
|
44
84
|
|
45
|
-
|
46
|
-
|
47
|
-
/* Customize the label (the container) */
|
48
|
-
|
49
|
-
.check-label {
|
50
|
-
|
51
|
-
display: flex;
|
52
|
-
|
53
|
-
justify-content: space-between;
|
54
|
-
|
55
|
-
|
85
|
+
</script>
|
56
|
-
|
57
|
-
cursor: pointer;
|
58
|
-
|
59
|
-
font-size: 1.5rem;
|
60
|
-
|
61
|
-
user-select: none;
|
62
|
-
|
63
|
-
border-bottom: 1px solid #a5a3a3;
|
64
|
-
|
65
|
-
margin: 10px 0 10px;
|
66
|
-
|
67
|
-
}
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
/* Hide the browser's default checkbox */
|
72
|
-
|
73
|
-
.check-label input {
|
74
|
-
|
75
|
-
position: absolute;
|
76
|
-
|
77
|
-
opacity: 0;
|
78
|
-
|
79
|
-
cursor: pointer;
|
80
|
-
|
81
|
-
}
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
/* Create a custom checkbox */
|
86
|
-
|
87
|
-
.checkmark {
|
88
|
-
|
89
|
-
height: 25px;
|
90
|
-
|
91
|
-
width: 25px;
|
92
|
-
|
93
|
-
}
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
/* On mouse-over, add a grey background color */
|
98
|
-
|
99
|
-
.check-label:hover {
|
100
|
-
|
101
|
-
background-color: #dcdcdc;
|
102
|
-
|
103
|
-
}
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
/* Create the checkmark/indicator (hidden when not checked) */
|
108
|
-
|
109
|
-
.checkmark:after {
|
110
|
-
|
111
|
-
content: "";
|
112
|
-
|
113
|
-
position: absolute;
|
114
|
-
|
115
|
-
display: none;
|
116
|
-
|
117
|
-
}
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
/* Show the checkmark when checked */
|
122
|
-
|
123
|
-
.check-label input:checked ~ .checkmark:after {
|
124
|
-
|
125
|
-
display: block;
|
126
|
-
|
127
|
-
}
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
/* Style the checkmark/indicator */
|
132
|
-
|
133
|
-
.check-label .checkmark:after {
|
134
|
-
|
135
|
-
position: relative;
|
136
|
-
|
137
|
-
left: 9px;
|
138
|
-
|
139
|
-
top: 5px;
|
140
|
-
|
141
|
-
width: 5px;
|
142
|
-
|
143
|
-
height: 10px;
|
144
|
-
|
145
|
-
border: solid #2196F3;
|
146
|
-
|
147
|
-
border-width: 0 3px 3px 0;
|
148
|
-
|
149
|
-
transform: rotate(45deg);
|
150
|
-
|
151
|
-
}
|
152
86
|
|
153
87
|
```
|
154
88
|
|
155
89
|
|
156
90
|
|
157
|
-
チェックボックスはlabel要素にて囲んでいるため、JavaScriptでチェックが入った時にlabelの背景色を変更することで対応できるかなと思ってJavaScriptを組んでみたのですが背景色が変更されず...
|
158
|
-
|
159
|
-
以下に書いてみたJavaScriptを載せますが、どこが間違っていますでしょうか。
|
160
91
|
|
161
92
|
|
93
|
+
コンソールでは確認4まで出力された後に、以下のメッセージが出力されています。
|
162
94
|
|
163
|
-
```
|
95
|
+
```
|
164
96
|
|
165
|
-
|
97
|
+
Uncaught TypeError: Cannot read property 'parentNode' of undefined
|
166
98
|
|
167
|
-
Myid=document.getElementById("material_select").getElementsByTagName('input');
|
168
|
-
|
169
|
-
if(Myid.checked == true){
|
170
|
-
|
171
|
-
|
99
|
+
at HTMLInputElement.buttons.(/anonymous function).onclick
|
172
|
-
|
173
|
-
Myid.parentNode.style.color = '#FFFFFF';
|
174
|
-
|
175
|
-
}
|
176
|
-
|
177
|
-
}
|
178
100
|
|
179
101
|
```
|
180
102
|
|
181
103
|
|
182
104
|
|
105
|
+
getElementsByTagNameの戻り値がリストなのでparentNodeでは親要素を取得できていないのかなというところまでは何となく分かったのですが、どのように対処すれば良いのかがいまいち分かりません...
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
|
183
|
-
|
111
|
+
どなたかご教授お願い致します。
|
1
情報追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -18,7 +18,7 @@
|
|
18
18
|
|
19
19
|
<span>テスト1</span>
|
20
20
|
|
21
|
-
<input type="checkbox" name="check" value="テスト1">
|
21
|
+
<input type="checkbox" name="check" onclick="javaScript:ChangeColor()" value="テスト1">
|
22
22
|
|
23
23
|
<span class="checkmark"></span>
|
24
24
|
|