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

質問編集履歴

1

コードを見やすくした

2020/11/11 06:20

投稿

ma_-
ma_-

スコア1

title CHANGED
File without changes
body CHANGED
@@ -1,42 +1,38 @@
1
1
  ### 前提・実現したいこと
2
2
  松 竹 梅というボタンを作ってそれを一度だけ押せる
3
-
4
- ここに質問の内容を詳しく書いてください。
3
+ HTML
5
- <script>
4
+ <div>
5
+ <input type="button" value="松" onclick="btnClick(0);" >
6
+ <input type="button" value="竹" onclick="btnClick(1);>
7
+ <input type="button" value="梅" onclick="btnClick(2);>
8
+ <h2 id="msg"></h2>
9
+ </div>
10
+ ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
11
+ 表示例
12
+
13
+ ![イメージ説明](21368f971dd65e017f12a370958bd59b.png)
14
+
15
+ ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
16
+ Javascript
17
+
6
- function btnClick(prm) {
18
+ function btnClick(prm) {
7
19
  var text = ['松','竹','梅'];
8
20
  var target = document.getElementsByClassName('select')
21
+
9
- for (var i = 0; i < target.length; i++) {
22
+          for (var i = 0; i < target.length; i++) {
23
+
10
- target[i].removeAttribute('disabled');
24
+          target[i].removeAttribute('disabled');
25
+
11
- }
26
+          }
12
- target[prm].setAttribute('disabled', 'disabled');
27
+ target[prm].setAttribute('disabled', 'disabled');
13
- document.getElementById("msg").textContent = text[prm];
28
+ document.getElementById("msg").textContent = text[prm];
14
- }
29
+ }
15
- </script>
30
+
16
- <body>
31
+
32
+
17
33
 
18
- <div>
34
+
19
- <input type="button" value="松" onclick="btnClick(0);">
35
+
20
- <input type="button" value="竹" onclick="btnClick(1);">
21
- <input type="button" value="梅" onclick="btnClick(2);">
22
- <h2 id="msg"></h2>
23
- </div>
24
- というプログラムを書いたのですが
25
- ボタンを押してもなにも表示されません
26
- どこが間違ってるか教えていた咲ければ幸いです
27
-
28
- ### 発生している問題・エラーメッセージ
29
- 10イベント.html:60 Uncaught TypeError: Cannot read property 'setAttribute' of undefined
30
- 何を試しても上記のエラーメッセージが出てきます
31
- ```
32
- エラーメッセージ
33
- ```10イベント.html:60 Uncaught TypeError: Cannot read property 'setAttribute' of undefined
34
-
35
- ### 該当のソースコード
36
- target[prm].setAttribute('disabled', 'disabled');
37
-
38
-
39
-
40
36
  ### 試したこと
41
37
 
42
38
  target[prm].setAttribute('disabled', 'disabled');これを消すと何回でも押せるボタンができたので引数が間違ってるのかと思いi,textなどを入れてみました