HTMLフォームの値を取得してJavaScriptでコンソールに表示しますか?
HTMLフォームの値を取得するには、valueプロパティを使用します。以下が入力タイプであるとしましょう-
<input type="text" id="getValues" value="My Name is John Smith" />
上記の値「私の名前はジョン・スミス」をコンソールに表示する必要があります。
例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initialscale=1.0">
<title>Document</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fontawesome/
4.7.0/css/font-awesome.min.css">
</head>
<body>
<input type="text" id="getValues" value="My Name is John Smith" />
<script>
var originalvalue = document.getElementById("getValues").value;
console.log("The value in the text box is ="+originalvalue);
</script>
</body>
</html> 上記のプログラムを実行するには、ファイル名「anyName.html(index.html)」を保存して、ファイルを右クリックします。 VSCodeEditorで[OpenwithLiveServer]オプションを選択します。
出力
これにより、次の出力が生成されます-
-
JavaScript-href値を取得
URLが次のアンカータグがあるとします- <a class="demo" title="get the url" href="./mainPage.jsp/1245">href value at console</a> URL値、つまりhref属性値のみを取得する必要があります。これには、attr()-を使用します attr('href') 例 以下はコードです- <!DOCTYPE html> <html lang="en"> <head&
-
JavaScriptにユーザーからのHTML入力を取得させ、解析して表示させますか?
HTML入力値は文字列です。文字列を整数に変換するには、parseInt()を使用します。 例 以下はコードです- <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> &nbs