Jonathan Ortega
Jonathan Ortega

Reputation: 9

Use array length as a variable

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>

Upvotes: 0

Views: 105

Answers (2)

Matt Burland
Matt Burland

Reputation: 45155

Another option is to just not bother setting the array to zero in the first place. You could do this:

var clickcount = [];

And then this:

function contarClick(id) {
    clickcount[id] = clickcount[id] ? clickcount[id] + 1 : 1;
}

On the first click clickcount[id] will be undefined, which is falsy so it'll get assigned 1. All other cases it will be incremented by 1.

Also, there is actually nothing wrong with your original code:

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;

alert(clickcount);
<a class="bannerlink">link1</a>

<a class="bannerlink">link2</a>

<a class="bannerlink">link3</a>

<a class="bannerlink">link4</a>
If you run the above snippet, it will alert 0,0,0,0 which is exactly what you'd expect.

Here's a working example using my suggestion:

var bannercount = document.getElementsByClassName("bannerlink");
var linkcount = bannercount.length;
var clickcount = []

function contarClick(id) {
    clickcount[id] = clickcount[id] ? clickcount[id] + 1 : 1;
  alert(clickcount);
}

var links = document.getElementsByClassName("bannerlink");

for (var i = 0 ; i < links.length; i++) {
  links[i].addEventListener("click", function() {
      contarClick(this.id);
    });
  }
<a class="bannerlink" id="0">link1</a>

<a class="bannerlink" id="1">link2</a>

<a class="bannerlink" id="2">link3</a>

<a class="bannerlink" id="3">link4</a>

Upvotes: 0

cppprog
cppprog

Reputation: 794

It seems that you just want clickcount to be full of 0's.

Delete this:

var clickcount = new Array(linkcount);
for (var i = 0; i < clickcount.length; i++)
    clickcount[i] = 0;

And replace with this:

var clickcount = [];
for(var i = 0; i < linkcount; i++){
    clickcount.push(0);
}

Upvotes: 3

Related Questions