質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

文字コード

文字コードとは、文字や記号をコンピュータ上で使用するために用いられるバイト表現を指します。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

Q&A

解決済

1回答

1428閲覧

Valueにセットされた文字列を正しく表示したい。

zoetti

総合スコア4

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

文字コード

文字コードとは、文字や記号をコンピュータ上で使用するために用いられるバイト表現を指します。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

0グッド

0クリップ

投稿2020/08/17 14:53

編集2020/08/18 23:56

前提・実現したいこと

ご教授お願いいたします。
クイズのWebアプリケーションを作成していて、Buttonを動的に作成しvalue属性に選択肢を記述しようとしています。
データはopen triviaというフリーのデータベースサーバからJson形式で取得しています。
value属性に選択肢をセットする際、取得してきたデータから選択肢部分を抽出してvalue属性に設定しようと思っています。

発生している問題・エラーメッセージ

value属性に選択肢をセットする際、取得してきたデータから選択肢部分を抽出してvalue属性に設定しようと試みましたが、
設定されたデータが実態参照をしてしまうため、正しく文字列が出力されません。

該当のソースコード

javascript

1const startBtn = document.getElementById('start'); 2const topContent = document.getElementById('topContent'); 3const centerContent = document.getElementById('centerContent'); 4const downContent = document.getElementById('downContent'); 5const header = document.getElementById('header'); 6const content = document.getElementById('content'); 7let questionData; 8let questionNumber = 1; 9const corrects = []; 10const incorrects = []; 11const correctAnswers = []; 12const questionAnswers = { 13 0: [], 14 1: [], 15 2: [], 16 3: [], 17 4: [], 18 5: [], 19 6: [], 20 7: [], 21 8: [], 22 9: [] 23} 24 25Array.prototype.shuffle = function () { 26  let i = this.length; 27  while (i) { 28   let j = Math.floor(Math.random() * i); 29   --i; 30   [this[i],this[j]] = [this[j],this[i]]; 31  } 32  return this; 33} 34 35startBtn.addEventListener('click', () => { 36 waitingPage(); 37 startBtn.remove(); 38 asynchronousFunc() 39 .then(fetchCorrectAnswers) 40 .then(fetchAnswers) 41 .then(changeQuestionPage); 42}); 43 44const asynchronousFunc = () => { 45 return new Promise((resolve, reject) => { 46 fetch('https://opentdb.com/api.php?amount=10') 47 .then(response => response.json()) 48 .then(data => { 49 questionData = data; 50 resolve(); 51 }) 52 }); 53} 54 55const fetchCorrectAnswers = () => { 56 for (let i = 0; i < 10; i++) { 57 const correctAnswer = questionData.results[i].correct_answer; 58 correctAnswers.push(correctAnswer); 59 } 60} 61 62const fetchAnswers = () => { 63 for (let i = 0; i < 10; i++) { 64 const correctAnswer = questionData.results[i].correct_answer; 65 const inCorrectAnswer = questionData.results[i].incorrect_answers; 66 inCorrectAnswer.forEach(item => { 67 questionAnswers[i].push(item); 68 }); 69 questionAnswers[i].push(correctAnswer); 70 questionAnswers[i].shuffle(); 71 } 72} 73 74const waitingPage = () => { 75 header.innerText = '取得中'; 76 content.innerText = '少々お待ちください'; 77} 78 79const changeQuestionPage = () => { 80 header.innerText = '問題1'; 81 createCategory(); 82 createLevel(); 83 createQuestion(); 84 createAnswersBtn(questionNumber - 1); 85 86} 87 88const createCategory = () => { 89 const category = questionData.results[0].category; 90 const genre = document.createElement('h3'); 91 const genreContent = document.createTextNode('[ジャンル] ' + category); 92 genre.appendChild(genreContent); 93 topContent.appendChild(genre); 94} 95 96const createLevel = () => { 97 const difficulty = questionData.results[0].difficulty; 98 const level = document.createElement('h3'); 99 const levelContent = document.createTextNode('[難易度] ' + difficulty); 100 level.appendChild(levelContent); 101 topContent.appendChild(level); 102} 103 104const createQuestion = () => { 105 const question = questionData.results[0].question; 106 content.innerHTML = question; 107} 108 109const createAnswersBtn = number => { 110 for (let i = 0; i < questionAnswers[number].length; i++) { 111 const answerBtn = document.createElement('input'); 112 answerBtn.type = 'button'; 113 answerBtn.name = 'answerBtn'; 114 answerBtn.value = questionAnswers[number][i]; **◀︎こちらになります。** 115 answerBtn.onclick = clickBtn; 116 downContent.appendChild(answerBtn); 117 } 118} 119 120const clickBtn = event => { 121 ++questionNumber; 122 header.innerText = '問題' + questionNumber; 123 topContent.children[1].innerText = '[ジャンル] ' + questionData.results[questionNumber - 1].category; 124 topContent.children[2].innerText = '[難易度] ' + questionData.results[questionNumber - 1].difficulty; 125 changeAnswersBtn(questionNumber - 1); 126} 127 128const changeAnswersBtn = number => { 129 while (downContent.firstChild) { 130 downContent.removeChild(downContent.firstChild); 131 } 132 createAnswersBtn(number); 133} 134

Json

1{response_code: 0, results: Array(10)} 2response_code: 0 3results: Array(10) 40: 5category: "Entertainment: Film" 6correct_answer: "The Thing" 7difficulty: "easy" 8incorrect_answers: (3) ["Carrie", "Misery", "The Green Mile"] 9question: "Which of the following movies was not based on a novel by Stephen King? " 10type: "multiple" 11__proto__: Object 121: {category: "Science & Nature", type: "multiple", difficulty: "medium", question: "Along with Oxygen, which element is primarily responsible for the sky appearing blue?", correct_answer: "Nitrogen",} 132: {category: "Entertainment: Film", type: "boolean", difficulty: "medium", question: "There aren&#039;t any live-action clones in &quot;…ar Wars: Episode III - Revenge of the Sith&quot;.", correct_answer: "True",} 143: {category: "Entertainment: Video Games", type: "boolean", difficulty: "hard", question: "In &quot;Portal 2&quot;, Cave Johnson started out Aperture Science as a shower curtain company.", correct_answer: "True",} 154: {category: "Geography", type: "multiple", difficulty: "medium", question: "Which of these countries is the smallest by population?", correct_answer: "Norway",} 165: {category: "Celebrities", type: "multiple", difficulty: "medium", question: "In what year did &quot;Bob Ross&quot; die?", correct_answer: "1995",} 176: {category: "General Knowledge", type: "multiple", difficulty: "medium", question: "Earl Grey tea is black tea flavoured with what?", correct_answer: "Bergamot oil",} 187: {category: "Celebrities", type: "multiple", difficulty: "medium", question: "Which American celebrity died in 1977 playing golf in La Moraleja, Madrid?", correct_answer: "Bing Crosby",} 198: {category: "Entertainment: Television", type: "multiple", difficulty: "easy", question: "On the NBC show Community, whose catch-phrase was &quot;Pop! Pop!&quot;?", correct_answer: "Magnitude",} 209: {category: "Science & Nature", type: "multiple", difficulty: "hard", question: "Autosomal-dominant Compelling Helio-Ophthalmic Out…drome is the need to do what when seeing the Sun?", correct_answer: "Sneeze",} 21length: 10 22__proto__: Array(0) 23__proto__: Object

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

maisumakun

2020/08/17 15:00

open triviaから得られるJSONはどのような形でしょうか?
zoetti

2020/08/17 15:08

Jsonデータを追記しました。 こちらの内容で問題ないでしょうか?
zoetti

2020/08/17 15:09

レスポンスヘッダーのコンテントタイプはこちらになります。 Content-Type: application/json
dameo

2020/08/19 14:34

input要素ではなくbutton要素にしてtype属性の設定をやめ、value属性で入れるのではなく、innerHTML属性で入れればbutton要素内のテキストとして入るので、正しく文字実体参照で表示できます。
zoetti

2020/08/19 14:39

dameoさんありがとうございます!! 先ほど自分もその結論にいたったところでした。 初歩的なミスすぎて恥ずかしいです...
guest

回答1

0

自己解決

inputではなくbuttonを使用した。

投稿2020/08/19 14:40

zoetti

総合スコア4

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問