Convert a string of text generated in a javascript file to an interactive link in HTML

Sorry for the incredibly lame question, but as a completely new programmer, all the answers that I found during the search all day concern other issues that are not related to me, so I decided that I would post about it.

I made a commitment to convert the sample “Random Quote Generator” that I found in a small web application that can create random links with the click of a button, but since I soon found that I was just replacing the examples with links of my choice, I created just plain text without a click.

I have tried many things related to mess with div and id tags but nothing works for me.

My code for html is as follows:

<html>
<head>
    <title>Simpsons Episode Generator</title>
</head>
<body>
    <h1>Simpsons Episode Generator</h1><br>
    <img src="https://vignette2.wikia.nocookie.net/simpsons/images/9/97/Mr_Burns_-_the_box.jpg/revision/latest?cb=20121215235014" alt="Mr Burns box">

    <div id="linkDisplay">
        <!-- Link will pop up here -->
    </div>

    <!-- Button to call the javascript and prduce a link -->
    <button onclick="newLink()">New Episode</button>

    <!-- javascript declared -->
    <script src="javascript.js"></script>
</body>
</html>

, , javascript , .

javascript :

//Links To Episodes
var links = [
    'http://kisscartoon.so/episodes/the-simpsons-season-1-episode-1/',
    'http://kisscartoon.so/episodes/the-simpsons-season-1-episode-2/',
    'http://kisscartoon.so/episodes/the-simpsons-season-1-episode-3/',
    'http://kisscartoon.so/episodes/the-simpsons-season-1-episode-4/',
    'http://kisscartoon.so/episodes/the-simpsons-season-1-episode-5/',
    'http://kisscartoon.so/episodes/the-simpsons-season-1-episode-6/'
];

//Select 1 Link at random and push it to the HTML
function newLink() {
    var randomLink = Math.floor(Math.random() * (links.length));
    document.getElementById('linkDisplay').innerHTML = links[randomLink];
}

, , , HTML , .

, , ...

+6
2

URL-, " ", :

window.location.href = links[randomLink];

, , <div> a <a>

<a id="linkDisplay" href='#' />

(# , ), href :

document.getElementById('linkDisplay').href = links[randomLink];
+1

HTML,

//Links To Episodes
var links = ['http://kisscartoon.so/episodes/the-simpsons-season-1-episode-1/',
  'http://kisscartoon.so/episodes/the-simpsons-season-1-episode-2/',
  'http://kisscartoon.so/episodes/the-simpsons-season-1-episode-3/',
  'http://kisscartoon.so/episodes/the-simpsons-season-1-episode-4/',
  'http://kisscartoon.so/episodes/the-simpsons-season-1-episode-5/',
  'http://kisscartoon.so/episodes/the-simpsons-season-1-episode-6/',]

//Select 1 Link at random and push it to the HTML
function newLink() {
  var randomLink = links[Math.floor(Math.random() * (links.length))];
  document.getElementById('linkDisplay').innerHTML = '<a href="'+randomLink+'" target="_blank">'+randomLink+'</a>';
}
<div id="linkDisplay">
  <!-- Link will pop up here -->
</div>


<!-- Button to call the javascript and prduce a link -->
<button onclick="newLink()">New Episode</button>
Hide result
+1

All Articles