Reputation: 37
Im writing a script that will have a quest randomly pop up when you refresh the page. Ive got the script working, but, the current one puts the texts at the top left-hand corner of the screen.
How do I convert what I already have to something like getElementById & innerHTML and place a span tag with an id="quest_id" inside the div I'd like the text to show up in?
Im a beginner at javascript, I know how to do most of these things by themselves, but integrating them together screws me up. Usually my problem is simple syntax I dont yet understand fully.
<script language="JavaScript">
var str = "click to go to the quest";
var quest = str.link("index.html");
var r_text = new Array ();
r_text[0] = "You found a quest! - " + quest;
r_text[1] = " ";
r_text[2] = " ";
r_text[3] = " ";
r_text[4] = " ";
r_text[5] = " ";
r_text[6] = " ";
r_text[7] = " ";
r_text[8] = " ";
r_text[9] = " ";
var i = Math.floor(10*Math.random())
document.write(r_text[i]);
//-->
</script>
Upvotes: 2
Views: 1316
Reputation: 333
Here, this should work, the 2nd script tag will load after the <body>
and the <div>
will be available.:
var str = "click to go to the quest";
var quest = str.link("index.html");
var r_text = new Array ();
r_text[0] = "You found a quest! - " + quest;
r_text[1] = "1 ";
r_text[2] = " 2";
r_text[3] = " 3";
r_text[4] = " 4";
r_text[5] = " 5";
r_text[6] = " 6";
r_text[7] = " 7";
r_text[8] = " 8";
r_text[9] = " 9";
var i = Math.floor(10*Math.random())
document.write(r_text[i]);
//-->
</script>
<body>
<div id="display">Here: </div>
</body>
<script language="JavaScript">
document.getElementById("display").innerHTML=r_text[i];
</script>
Upvotes: 2