0

I have a 2d array of images called "onClickCard",

What I'm trying to do is make is so that every time I click one of the images in the 2d array I increment my variable called "click" by 1.

running the following:

    var click = 0; 
    var click=onclick.click+1

I just get a console error stating "Uncaught TypeError: Cannot read property 'click' of null: onClickCard, onclick"

edits:

I tried the below suggestion of

var click = 0;
window.onclick=function(){click ++;}

and each click is still returning 0.

3
  • 2
    You can't click a variable! Your code is trying to do that... Commented Sep 27, 2013 at 22:26
  • click+=1; inside your onClick function would do it. You need to have a onClick function for the element being clicked.. Commented Sep 27, 2013 at 22:26
  • Which browser did you use? This is the same code but with alert() and you can click anywere, please try jsfiddle.net/Bum6L/2/show Commented Sep 27, 2013 at 22:40

1 Answer 1

1

You can't click on variables, you can add event/click listeners on elements. Since you don't refer any element I assume window on my example and attach a click event listener on it that runs a function were the click variable is added +1.

Try this:

var click = 0; 
window.onclick=function(){click ++;}

Demo here

Sign up to request clarification or add additional context in comments.

3 Comments

@user2825148, in jsfiddle you need to click inside the window on the right-left. I tried it before posting.
@user2825148, here is the same but with alert(), just click anywere - jsfiddle.net/Bum6L/2/show
Thanks guys this has helped me to progress a bit especially your jsfiddle link.

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.