Warstolrem Warstolrem - 3 months ago 6
HTML Question

Functions not running when called?

I'm having trouble getting my javascript code to work. My goal with it is to be able to add new television series links to a database of some sort, and then open them all in seperate tabs when I click on a button. However, as of now, when I click the add series button, it doesn't save into the urlArray. Help?

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
function AddSeries() {
url = getElementById('serieslink');
urlArray = [];
urlArray.push(url);
}
<script type="text/javascript">
function clickyClick() {
for (url in urlArray) {
window.open(url,'_blank');
}
}
</head>
<body>
<form> Series Name:
<input type = "text"
name = "series1name"
id = "seriesname" > <br>
Series Link:
<input type = "text"
name = "series1link"
id = "serieslink" >
</form> <br>
<button onclick = "AddSeries()" > Add A New Series </button><br>
<button onclick = "clickyClick()" > Open Incomplete Series </button>




Answer

1- You should close the script tags.

2- You don't need to open separate script tags for sequential scripts.

3- You have to change the scope of the urlArray to global. You can do it by adding a var before the definition and outside of the function.

<script type="text/javascript">   
var    urlArray = [];
function AddSeries() {
    url = getElementById('serieslink');
    urlArray.push(url);
}

function clickyClick() {
    for (url in urlArray) {
      window.open(url,'_blank');
    }
}
</script>