I'm having a bad time trying to push some values to an array.
Here's the concept:
1. With the press of a button, I am adding input boxes to the page
function addAnInput(){
document.getElementById("new").innerHTML += 'Insert link: <input id="gate' + gate + '" type="text" size="120%" /><br />';
gate += 1;
count.push(gate.toString());
}
This allows me to infinitely add boxes to the page. I use the boxes to input some links.
2. A second button, should normally read the values in the boxes and push them in an Array through a for loop.
function whichOne(){
gatePush = 0;
daLink = [];
for (i = 0; i < count.length; i += 1){
function whatPush(){return document.getElementById("gate" + gatePush).value}
var thisPush = whatPush();
daLink.push(thisPush);
gatePush += 1;
};
}
3. The second for loop, it should choose randomly a value from the Array and use it in order to open the designated link.
for (i = 0; i < howMany; i += 1){
function thisNow(){return Math.floor(Math.random() * daLink.length);}
var thisOne = thisNow();
if (daLink[thisOne] != "")
window.open(daLink[thisOne], "_blank");
};
But, as I stood awake all night and tried to rewrite over and over again, I just can't seem to run the code correctly. It seems to be a problem in pushing the input values into the 'daLink' array. Can anyone tell me what am I doing wrong here?
Here is the full script for a better throughout view:
- one button for the addAnInput() function;
- one button for the whichOne()function;
var count;
var howMany = document.getElementById("how-many").value;
var gate = 0;
var gatePush;
var daLink;
//==============================================================================
//
//Using the 'gate' variable in order to give a different id for each new input.
//
//Need the 'gate' value pushed into 'count' as string for later use.
//
//==============================================================================
function addAnInput(){
document.getElementById("new").innerHTML += 'Insert link: <input id="gate' + gate + '" type="text" size="120%" /><br />';
gate += 1;
count.push(gate.toString());
}
//==============================================================================
//
//During each loop, the 'whatPush()' function is calling the value of each input
//and stores it in the 'thisPush' variable.
//The stored value is then pushed in the 'daLink' array
//
//==============================================================================
function whichOne(){
gatePush = 0;
daLink = [];
for (i = 0; i < count.length; i += 1){
function whatPush(){return document.getElementById("gate" + gatePush).value}
var thisPush = whatPush();
daLink.push(thisPush);
gatePush += 1;
};
//=============================================================
//
//Once the 'daLink' array has stored the values, the for loop opens
//a new tab randomly using one of the stored values.
//
//=============================================================
for (i = 0; i < howMany; i += 1){
function thisNow(){return Math.floor(Math.random() * daLink.length);}
var thisOne = thisNow();
if (daLink[thisOne] != "")
window.open(daLink[thisOne], "_blank");
};
}
Aucun commentaire:
Enregistrer un commentaire