1 Replies - 343 Views - Last Post: 09 December 2011 - 03:17 PM

#1 carlbrooks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 19-November 11

2 issues with number of buttons selected

Posted 09 December 2011 - 11:30 AM

Got 2 problems with my buttons selection.

What happens is the user selects the number of buttons depending on the number entered in the text box. If the number is 3 in the text box, then the user can only select 3 buttons, if more buttons are selected then it comes with an alert message saying user is beyond limit deselect a button to be able to choose another button.

But these are the problems I have encounted:

If I type in "2" in the textbox for example, it allows me to select 2 buttons and comes up with alert if more buttons clicked.

problem 1: but if I change the figure in text box from "2" to "5", then it only allows me to select 3 buttons (I think it is adding 2 from the previous value and 3 to make the current value 5)

problem 2: If I enter a value less than current value, so in this example if I enter in 1 in the textbox which is obviously less than 2, then it lets the user select unlimited number of buttons.

So does anyone know how to fix these problems so that the amount of buttons selected matches correctly to the value in the textbox?

code is in jsfiddle, click [here][1]

Thanks


[1]: http://jsfiddle.net/7WwaK/10/

Is This A Good Question/Topic? 0
  • +

Replies To: 2 issues with number of buttons selected

#2 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 800
  • View blog
  • Posts: 1,686
  • Joined: 30-January 09

Re: 2 issues with number of buttons selected

Posted 09 December 2011 - 03:17 PM

It came down to the way you were declaring the currenttotal variable, and the fact that you weren't refreshing it when value of numberAnswerText changed. This code works:
Spoiler

I also deliver this code with a caveat - using global variables is a bad idea. A better solution to the problem would be not using the currenttotal variable at all, but instead determining the currenttotal at the start of the btnclick() function by counting the number of DOM elements that have the class "answerBtnsOff".

This post has been edited by e_i_pi: 09 December 2011 - 03:22 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1