#目的
javascriptで全角英数字を半角英数字に変換するアプリを作ろうとしています.
replaceメソッドを使って全角文字を半角文字に変換しようとしているのですが,"replace is not a function"とエラーが出てしまいます.
なぜこのようなエラーが出てしまうのか,また解決方法を教えていただきたいです.
よろしくお願いいたします.
#エラーメッセージ
Uncaught TypeError: text.replace is not a function
at HTMLButtonElement.<anonymous> (main.js:6)
(anonymous) @ main.js:6
#ソース
html
1<!DOCTYPE html> 2<html> 3 <head> 4 <meta charset="utf-8"> 5 <title>全角数字 -> 半角数字</title> 6 <link rel="stylesheet" href="styles.css"> 7 </head> 8 <body> 9 全角英数字を半角英数字に直します 10 <table> 11 <tr> 12 <td><textarea id="text" rows="30" cols="80" placeholder="ここにテキストを入力"></textarea></td> 13 <td>>>></td> 14 <td><textarea id="result" rows="30" cols="80" placeholder="結果が表示されます"></textarea></td> 15 </tr> 16 </table> 17 <button type="button" id="btn">実行</button> 18 <script src="main.js"></script> 19 </body> 20</html>
javascript
1(function () { 2 'use strict'; 3 4 document.getElementById('btn').addEventListener('click', function(){ 5 document.getElementById('text').value; 6 text.replace(/[A-Za-z0-9]/g, function(s) { 7 return String.fromCharCode(s.charCodeAt(0) - 65248); 8 }); 9 document.getElementById('result').value += text; 10 }); 11 12})();

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/12/16 06:59
2017/12/16 07:09 編集
退会済みユーザー
2017/12/16 07:18