Convert a line of text generated in a javascript file into a clickable link in HTML -
sorry incredibly lame question, brand new programmer answers i've found while searching day other problems not specific me decided i'd post it.
i have taken upon myself convert sample "random quote generator" program found small web based app can produce random links @ touch of button, found out replacing example quotes links of choice produced normal un-clickable text.
i have tried many things relating messing div's , id tags nothing works me.
my code html 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 pop here --> </div> <!-- button call javascript , prduce link --> <button onclick="newlink()">new episode</button> <!-- javascript declared --> <script src="javascript.js"></script> </body> </html>
so can see, calls javascript run randomiser prints result.
the javascript follows:
//links 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 @ random , push html function newlink() { var randomlink = math.floor(math.random() * (links.length)); document.getelementbyid('linkdisplay').innerhtml = links[randomlink]; }
all want make click html receiving after button pressed clickable.
thanks reading , sorry asking such simple sounding question have been looking day how myself...
if want navigate url, don't need "click link", can change location of window:
window.location.href = links[randomlink];
if want make link location, people can click it, make <div>
<a>
<a id="linkdisplay" href='#' />
(the #
means go top of page) , update href
needed:
document.getelementbyid('linkdisplay').href = links[randomlink];
Comments
Post a Comment