0

Trying to increment numbers with a click, you can see by clicking my jsfiddle the issue that I am having, first two clicks register 0 and then the numbers increase 10 at a time afterwards

http://jsfiddle.net/jamcrowe/LvwDb/1/

var online = 0;
var creative = 0;
var technical = 0;
var analyst = 0;
var managerial = 0;

function  me() {
    $('#one').click(function(){
        creative +=5;
        online ++;
        managerial ++;
    });
    $('#two').click(function(){
        creative +=5;
        online ++;
        managerial ++;
    });
    $('#three').click(function(){
        creative +=5;
        online ++;
        managerial ++;
    });
    alert(creative, online, managerial);
}
4
  • You're assigning the click handlers multiple times. You don't need me function at all jsfiddle.net/LvwDb/2 Commented Feb 20, 2014 at 2:11
  • What do u suggest for a resolve? first time trying to develop with increments. Appreciate the feedback! Commented Feb 20, 2014 at 2:13
  • have you checked the link I provided? "first time trying to develop with increments" --- it has nothing to do with them actually, but about how you deal with event handlers. Commented Feb 20, 2014 at 2:15
  • Yes resolved the issue, Cheers dude!! Commented Feb 20, 2014 at 2:18

1 Answer 1

1

You are binding the events in jQuery AND in the DOM. I'm not entirely sure what you're looking for but I'm guessing it's something like this: http://jsfiddle.net/wnfBn/

var online = 0;
var creative = 0;
var technical = 0;
var analyst = 0;
var managerial = 0;

$('#one,#two,#three').click(me);
function me() {
    creative += 5;
    online++;
    managerial++;
    alert(creative + ':' + online + ':' + managerial);
};
Sign up to request clarification or add additional context in comments.

7 Comments

Awesome, this is exactly what I was looking for, If I was looking to pass these values into an array which way would I go about it
I'm not sure I follow what you mean by "pass these values into an array"
Assume I had ten divs and each time a div is clicked the incremented number would go into an array, allowing to print the value of eg div three, by getting it through the array[2] , sorry if that doesnt make sense
"You are binding the events in jQuery AND in the DOM" --- in javascript and html. All the event handlers are bound in DOM.
If you look at the fiddle link @zerkms I removed the click events from the dom
|

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.