I have a JavaScript array and I wanted to put HTML links in that array. I don't know if there is a certain way to go about this. Also, if it's possible, when the user clicks the button and it selects the link, is there a way to make that link open up in a new tab (I know in HTML its target=_blank). I appreciate any help as always.
JavaScript:
var i = Math.floor(Math.random() * 10) +1;
function randomPageWithJS() {
const arrayJS = [link1, link2, link3, link4];
document.getElementById('randomPageWithJS').value = arrayJS[i];
}
randomPageWithJS();
HTML:
<form>
Random Song:<input type="text" id="randomPageWithJS" name="randomPage"/>
<input type="button" value="RandomPage" onclick="randomPageWithJS()" width="1000"/>
</form>
Use window.open
window.open(arrayJS[i], '_blank');
Example:
function randomPage() {
const links = ['https://google.com', 'https://stackoverflow.com', 'https://stackoverflow.com/questions/56139010/'];
window.open(links[Math.floor(Math.random()*links.length)], '_blank');
}
<input type="button" value="Random Page" onclick="randomPage()">