実現したいこと
Cannot read properties of null (reading 'classList')のエラーを解決したい
発生している問題・分からないこと
フォームを入力し、条件を満たしている場合にページ遷移するというシステム実装中にエラーが発生する
エラーメッセージ
error
1Uncaught TypeError: Cannot read properties of null (reading 'classList')
該当のソースコード
html
1<!DOCTYPE html> 2 3<html lang="ja" xmlns="http://www.w3.org/1999/xhtml"> 4<head> 5 <meta charset="utf-8" /> 6 <title>モバイルオーダー</title> 7 8</head> 9<body> 10 <form method="post" action="main.html"> 11 <p>名前</p> 12 <input type="text" maxlength="100" placeholder="山田太郎" id="name" autocomplete="name"> 13 <p>学籍番号</p> 14 <input type="password" maxlength="20" placeholder="7桁の学籍番号" id="pass" autocomplete="pass" > 15 <p>商品</p> 16 <input type="text" maxlength="7" id="food" autocomplete="food"> 17 <p><input type="submit" value="注文を行う" id="order"></p> 18 19 <script src="main.js"></script> 20</body> 21 22</html>
Javascript
1document.getElementById("order").onclick = function() { 2 const name = document.getElementById("name").value; 3 const pass = document.getElementById("pass").value; 4 const food = document.getElementById("food").value; 5 6 var flag = 0; 7 if(name.length == 0 || pass.length == 0 || food.length == 0) { 8 flag = 1; 9 } 10 11 if(flag == 1) { 12 alert('必須項目が未記入の箇所があります'); 13 } else { 14 var regexp = /^\d{7}$/; 15 if(regexp.test(pass) != true) { 16 alert('学籍番号が7桁になっていません'); 17 } else { 18 window.location.href='callback.html'; 19 console.log("使えている"); 20 } 21 } 22 };
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
多くの場合ではhtmlのidとJavascriptでのidがタイプミスなどによって異なっている場合に表示されるエラーであるそうなのですが、自分ではそのようなミスを見つけることができなかった
補足
特になし

回答2件
あなたの回答
tips
プレビュー