javascript - Image from array is not appended to <div> id -


i have array of images , when content page loaded. 1 picture array appended <div> id , displayed.

var gamequestion = ["a?", "b?", "c?"];    var gameanswer = [    ["lib/true.png", "lib/false.png"],    ["lib/true.png", "lib/false.png"],    ["lib/true.png", "lib/false.png"]  ];    var answerlist,    random_question;    random_question = math.floor(math.random() * gamequestion.length);    $("#question").html(gamequestion[random_question]);    answerlist = gameanswer[random_question];    $("#answer_1").html(answerlist[0]);  $("#answer_2").html(answerlist[1]);
<!-- original question -->  <div id="question" style="position:absolute; z-index:6; top:750px; left:160px; margin:auto; color:#ffffff; font-size:30px; width:800px; text-align: center;"></div>    <!-- answer-original-choice list -->  <div id="answer_1" class="answers" style="position:absolute; z-index:6; top:1006px; left:224px; margin:auto; color:#ffffff; font-size:25px; width:750px;"></div>  <div id="answer_2" class="answers" style="position:absolute; z-index:6; top:1186px; left:224px; margin:auto; color:#ffffff; font-size:25px; width:750px;"></div>

when content page loads, able see question, however, part image should appended , displayed. showing file path name.

hence, if content page loads question: a?, 2 image id should load , display image of true & false. @ point, seeing file path name of "lib/true.png" , "lib/false.png" img id "answer_1" , "answer_2".

therefore, wrong? need img ids display image instead of showing file path name.

please help. thankyou

you'll have create <img> src attribute. there several ways to this, of creating html string pretty straight forward one:

$("#answer_1").html("<img src='" + answerlist[0] + "'/>") 

this create string "<img src='lib/false.png'/>". jquery html method put inside #answer_1 div , browser parse html.

a better approach might create <img> tags , update src attribute. html like:

<div><img id="answer_1" /></div> 

with js:

$("#answer_1").attr("src", answerlist[0]); 

now, browser doesn't have remove/inject new image every time this.

(note images won't work in snippet)

var gamequestion = ["a?", "b?", "c?"];    var gameanswer = [    ["lib/true.png", "lib/false.png"],    ["lib/true.png", "lib/false.png"],    ["lib/true.png", "lib/false.png"]  ];    var answerlist,    random_question;    random_question = math.floor(math.random() * gamequestion.length);    $("#question").html(gamequestion[random_question]);    answerlist = gameanswer[random_question];    $("#answer_1").html("<img src='" + answerlist[0] + "'/>");  $("#answer_2").html("<img src='" + answerlist[1] + "'/>");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <!-- original question -->  <div id="question" style="position:absolute; z-index:6; top:750px; left:160px; margin:auto; color:#ffffff; font-size:30px; width:800px; text-align: center;"></div>    <!-- answer-original-choice list -->  <div id="answer_1" class="answers" style="position:absolute; z-index:6; top:1006px; left:224px; margin:auto; color:#ffffff; font-size:25px; width:750px;"></div>  <div id="answer_2" class="answers" style="position:absolute; z-index:6; top:1186px; left:224px; margin:auto; color:#ffffff; font-size:25px; width:750px;"></div>


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -