innerHTMLでテキストボックスに入力された文字を表示しようと
<script>
function sample1(){
var tx1 = document.getElementById("txt1").value;
document.getElementById("area1").innerText = tx1;
}
</script>
<script>
function sample2(){
var tx2 = document.getElementById("txt2").value;
document.getElementById("area2").innerText = tx2;
}
</script>
<script>
function sample3(){
var tx3 = document.getElementById("txt3").value;
document.getElementById("area3").innerText = tx3;
}
</script>
<script>
function sample4(){
var tx4 = document.getElementById("txt4").value;
document.getElementById("area4").innerText = tx4;
}
</script>
<script>
function sample5(){
var tx5 = document.getElementById("txt5").value;
document.getElementById("area5").innerText = tx5;
}
</script>
<input type="text" id="txt1" value="" style="width:10%;margin:5px;"><input type="text" id="txt2" value="" style="width:10%;margin:5px;"><input type="text" id="txt3" value="" style="width:10%;margin:5px;"><input type="text" id="txt4" value="" style="width:10%;margin:5px;"><input type="text" id="txt5" value="" style="width:10%;margin:5px;">
<br>
<input type="button" value=" 1変更↓ " onclick="sample1();" style="width:10%;margin:5px;"><input type="button" value=" 2変更↓ " onclick="sample2();" style="width:10%;margin:5px;"><input type="button" value=" 3変更↓ " onclick="sample3();" style="width:10%;margin:5px;"><input type="button" value=" 4変更↓ " onclick="sample4();" style="width:10%;margin:5px;"><input type="button" value=" 5変更↓ " onclick="sample5();" style="width:10%;margin:5px;">
<script>
document.writeln(" <div id=\"area1\" class=\"result\" style=\"margin:5px;\"> </div> ");
document.writeln(" <div id=\"area2\" class=\"result\" style=\"margin:5px;\"> </div> ");
document.writeln(" <div id=\"area3\" class=\"result\" style=\"margin:5px;\"> </div> ");
document.writeln(" <div id=\"area4\" class=\"result\" style=\"margin:5px;\"> </div> ");
document.writeln(" <div id=\"area5\" class=\"result\" style=\"margin:5px;\"> </div> ");
</script>
と書いたのですが同じコードを5回コピペする形なのでforでまとめたいのですがどうすればいいでしょうか?教えていただきたいです。
No.1ベストアンサー
- 回答日時:
こんにちは
回答がないようなので・・・
というよりも、
>forでまとめたいのですが~~
までわかっているなら、構文さえ調べれば、もうできたようなものだと思いますが…?
https://developer.mozilla.org/ja/docs/Web/JavaSc …
例が欲しいというのであれば、例えば、ご提示の「document.writeln」の塊の部分で考えてみれば
for(let i=1; i<=5; i++){
document.writeln(`<div id="area${i}" class="result" style="margin:5px;"></div>`);
}
みたいな要領でしょうか。
ざっと見て、ご提示のサンプルはHTMLにも繰返しが多いので、そちらも含めて繰返しで処理する例を以下に挙げておきます。
記述方法や仕組みはいろいろ考えられると思いますので、決して例示が唯一というわけではありません。
また、当面の処理には不要な個々のidやクラス、及び、HTML内のstyle設定は削除しました。
<!DOCTYPE HTML>
<html lang="ja">
<head><title>Sample</title>
<style type="text/css">
#texts input, #buttons input { box-sizing:border-box; width:10vw; margin:5px; }
#areas div { margin:5px; }
</style>
</head>
<body>
<div id="texts"></div>
<div id="buttons"></div>
<div id="areas"></div>
<script type="text/javascript">
//繰り返しの回数
const setNo = 5;
//関数定義
let addElm = (sel, str)=>{document.getElementById(sel).insertAdjacentHTML('beforeend', str);}
//DOM追加
for(let i=1; i<=setNo; i++){
addElm('texts','<input type="text" value="" />');
addElm('buttons',`<input type="button" value="${i}変更↓" />` );
addElm('areas', '<div class="result"></div>');
}
//イベント処理
document.getElementById('buttons').addEventListener('click', (e)=>{
let t = e.target;
if(t.nodeName == "INPUT" && t.type == "button"){
let n = Array.prototype.indexOf.call(document.querySelectorAll('#buttons>input'),t);
let v = document.querySelectorAll('#texts>input')[n].value;
document.querySelectorAll('#areas>div')[n].innerHTML = v;
}
});
</script>
</body>
</html>
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
似たような質問が見つかりました
- JavaScript コードレビューをお願いします。 1 2022/07/16 05:38
- HTML・CSS CSSが効かずどのように指定すれば良いか分からないのでアドバイスお願い致します 2 2023/06/07 12:25
- JavaScript ①入力フォーム→②確認表示画面→③送信完了画面のコードを書いているのです、 入力フォームから受け取っ 2 2022/05/10 16:45
- JavaScript プログラムがうまく動きませんレビューお願いします 1 2022/07/10 05:08
- JavaScript 入力フォームの javascript で メールアドレスの正規チェックをを行い、ボタンをクリックして 2 2022/04/27 16:06
- HTML・CSS ボタンをクリックした時に、入力フォームのすぐ下部に、「入力欄が空白です」というテキストメッセージが表 1 2022/04/27 16:25
- JavaScript 画像の表示位置 3 2022/12/23 08:25
- JavaScript clear機能を失わずにファイルアップロード機能を作成したい 3 2023/06/10 16:12
- HTML・CSS CSS のみのタブ切り替えについて 1 2023/01/11 16:47
- JavaScript ソースコードのいじる場所が分かりません。 1 2022/12/23 02:06
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
文字を固定したいのですが…
-
離れた場所にマウスオーバーで...
-
1行で左寄せと右寄せと中央揃え...
-
オンマウス時に別画像を上に重...
-
文字をクリックしたら別の文字...
-
CSS <div>の入れ子が反映さ...
-
レイアウトが崩れないようにす...
-
clear: bothの事で質問です。
-
CSS 可変マージン
-
★★★フッター最下部固定/スクロ...
-
スクロール可能なチェックボックス
-
MAX関数を使ってからLEFT JOIN...
-
htmlの記述で link rel=styles...
-
画像の上に画像リンクを貼る方法
-
スライダーを実装した場合、ペ...
-
背景色を一定時間ごとにランダ...
-
javascriptでURLにマウスオーバー
-
[急ぎ] videoタグで埋め込んだm...
-
文字と数字が混在する要素のsor...
-
ホームページビルダー15 メニュ...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
Ctrl+F(検索)の窓を出したいの...
-
フッター上部に謎の隙間
-
【CSS】floatで左右に並べた...
-
HTMLですCSSです この画像のよ...
-
css固定したフッターが本文と重...
-
CSS <div>の入れ子が反映さ...
-
スクロール可能なチェックボックス
-
textareaで入力した文字を改行...
-
オンマウス時に別画像を上に重...
-
文字を固定したいのですが…
-
スタイルシートで画面を上下に...
-
チェックボックスの背景色って...
-
フッターの下に隙間ができてしまう
-
文字をクリックしたら別の文字...
-
大分類・中分類・小分類
-
HTMLですCSSです 画像のように...
-
CSSでdivのheightを動的に
-
★★★フッター最下部固定/スクロ...
-
離れた場所にマウスオーバーで...
-
htmlのstyleのposition:relativ...
おすすめ情報