/    Sign up×
Community /Pin to ProfileBookmark

2 issues with number of buttons selected

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]: [url]http://jsfiddle.net/7WwaK/10/[/url]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@pactor21Dec 09.2011 — You will need to reset your 'currenttotal' variable when the number put into the #numberAnswerTxt input box gets changed.

Using your own example, you start with 2. You are able to select 2 of the letters and alert box pops up when more than 2 are clicked. All is well.

Then you change the number to 5. At this point your 'currenttotal' is still 2. You're fine until you select 3 letters, but when you go ahead and click another letter on the table, your 'currenttotal' is now 6 stopping your selection as your js code is supposed to.

For the other example for decreasing the number from 2 to 1, same situation happens until the first 2 selection of letters. After you change the number 1, your 'currenttotal' is still the same 2 with 1 in the #numberAnswerTxt. So your if comparison condition fails, and that alert box never shows up.
×

Success!

Help @carlbrooks spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...