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/

Recommended Answers

All 2 Replies

change you code around a little, move your currenttotal declaration to above the get_buttons call, and add currenttotal=0 to the end of that call, this should be a good place to start:

// move this above the function getButtons call.
var currenttotal = 0;
function getButtons() {
    for(var i = 65; i <= 90; i++) { // iterate over character codes for A to Z
        var letter = String.fromCharCode(i);
        document.getElementById("answer" + letter).className = "answerBtnsOff";
    }
    document.getElementById("answerTrue").className = "answerBtnsOff";
    document.getElementById("answerFalse").className = "answerBtnsOff";
    document.getElementById("answerYes").className = "answerBtnsOff";
    document.getElementById("answerNo").className = "answerBtnsOff";
    /**** reset your current total if they changed the amount in the input box.
     you were resetting all of the buttons anyway.  ***/
    currenttotal = 0;
    
    // code above makes sure all buttons start off with class answerBtnsOff, (so all button are white).
}

Just how many versions of this question are there?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.