質問するログイン新規登録

質問編集履歴

2

内容の修正

2018/05/09 07:11

投稿

science_mac
science_mac

スコア29

title CHANGED
File without changes
body CHANGED
@@ -1,92 +1,56 @@
1
- labelタグで囲ったチェックボックスチェックた時に、背景色を変更したいです。
1
+ チェックボックスチェックを入れた時に、label属性の背景色を変更したいです。
2
+ 現在、以下のような形で実装を試みています。
2
3
 
3
- 現在、以下形で実装しています。
4
+ ※label内チェックボックスは複数実装しています。
4
-
5
- ``` PHP
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" onclick="javaScript:ChangeColor()" value="テスト1">
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
- ```css
22
+ ```JavaScript
23
+ <script>
20
- .check-label-container {
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
- width: 50%;
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
- /* Create a custom checkbox */
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
- チェックボックスはlabel要素にて囲んでいるため、JavaScriptでチェックが入った時にlabelの背景色変更すること対応でかなと思ってJavaScriptを組んたのですが背景色が変更さ...
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

情報追加

2018/05/09 07:11

投稿

science_mac
science_mac

スコア29

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>