0

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>
3
  • You don't need to set the size of the array. Just do var clickcount = [] to create an empty array and in your for loop just do for (var i = 0; i < linkcount; i++) Commented Sep 19, 2014 at 17:50
  • BTW: not sure what you are doing with contador but it doesn't seem necessary. A simple clickcount[id]++ should suffice. Commented Sep 19, 2014 at 17:58
  • When you say it "doesn't work", what do you mean? What actually happens? Because it actually works just fine. There is no problem with using the array length as an integer because that's exactly what it is. Commented Sep 19, 2014 at 18:10

2 Answers 2

3

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

You don't actually have to use push. You can just index it as they had before (clickcount[i]=0) and it would work. Having said that, I prefer to use push.
0

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>

6 Comments

Thanks for your answer, it will help me get it done without the for, but I don't know if this will increment separately, because I want to save each a href click on a separate countdown. The error I keep getting is that linkcount value was 0, and it was supposed to be 2, dont know why
@JonathanOrtega: but I don't know if this will increment separately it will. It's still an array.
I update the code but it displays ,1 ,,11 ,,,111 each time, it doesn't increment the value
@JonathanOrtega: That looks like you are doing string concatenation
I think the previous suggestion is not working properly, I edited my post with my new code, could you please advise me if something is incorrect, I'm kind of new to javascript
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.