Hi my question is if I can use the length of an array as a integer value, here is the code
var contador = 0;
var bannercount = document.getElementsByClassName("bannerlink");
var linkcount = bannercount.length;
var clickcount = new Array(linkcount);
for (var i = 0; i < clickcount.length; i++)
clickcount[i] = 0;
function contarClick(id) {
contador = clickcount[id];
contador++;
clickcount[id] = contador;
contador = 0;
//var imagen = $(identificador + 'img');
//var enlace = document.getElementById(imagen).getAttribute('href');
//alert('tomar conteo:' + contador + ' tomar url' + enlace);
alert("Total de Enlaces en Página: " + linkcount + "\nConteo de Click: " + clickcount[id] + "\nIdentificador de este Enlace: " + id + "\nTamaño del Arreglo: " + clickcount.length);
}
I know its kind of messy and hard to understand because it on spanish, so I will explain it: bannercount variable gets all the elements on my HTML that have 'bannerlink' class, if I use alert(bannerlink.length) it does show a message with the number of elements, but I can't use it on other code.
What I want is that linkcount variable gets the length of the bannercount variable and set it as the size of clickcount array. The rest of my function works perfectly fine if I set linkcount as a number, but when I insert the bannercount.length it doesn't work. I don't understand why, because when I use it on the for with the other array it does work.
This script is supposed to count the amount of clicks of my href tags, depending on the class I asign for them, it does work fine if I manually set the array size but I want it to be automated, any suggestions?
Edit two hours later, with your recommendations, even if it doesn't work quite well, it has improved a bit, but the clickcount is not incrementing correctly, here is my new code, thanks for all your answers. The counter of href should start separately, its for keep record of the amount of clicks on each href, then I will send to my SQL database (God knows how because javascript can't connect to mySQL but I will figure something). Also it affects that Im running it on ASP.NET?
<script type="text/javascript">
var bannercount = document.getElementsByClassName("bannerlink");
var clickcount = [];
clickcount.length = bannercount.length;
function contarClick(id) {
clickcount[id] = clickcount ? clickcount + 1 : 1;
alert("Total de Enlaces en Página: " + bannercount.length + "\nConteo de Click: " + clickcount[id] + "\nIdentificador de este Enlace: " + id + "\nTamaño del Arreglo: " + clickcount.length);
}
</script>
var clickcount = []to create an empty array and in yourforloop just dofor (var i = 0; i < linkcount; i++)contadorbut it doesn't seem necessary. A simpleclickcount[id]++should suffice.