Skip to main content
Participant
July 31, 2018
Answered

Using javascript to check boxes based on input in a textbox?

  • July 31, 2018
  • 1 reply
  • 1824 views

Hello world,

I have a problem with a form I'm trying to put together; the form will be used to tally the amount of trucks coming into a job site (I.E. it is a grid with checkbox items that are ticked as trucks come in). The problem is, I'd also like to add the option of having a textbox with a value that will automatically tick the right amount of checkbox items, rather than checking them all one by one.

I guess in summary the code would need to be like this: inputs > 0, Check Box 1 is checked. inputs > 1, Check Box 2 is checked, etc.

I have no real experience with javascript, so any help would be appreciated,

Thanks,

This topic has been closed for replies.
Correct answer try67

"That's not a very good naming scheme. I would recommend to use numbers only, unless there's a very good reason to do it differently.

I didn't quite follow the second answer. Let's say you have 10 check-boxes and 1-6 are ticked. The user enters "5" into the text box. What should happen?"

Okay, I see what you're getting at, in that situation the 6th check box should not be checked; if it could be worked in to un-checked that 6th box, then I would prefer that.

I can change the naming scheme, its really on there only as a default for each box.


So basically you want to reset the entire set of fields, and then only tick those that were entered in the text field, right?

In that case, you can use this code, assuming you have fields named "Check Box 1" to "Check Box 10":

if (event.value) {
    for (var i=1; i<=10; i++) {

          this.getField("Check Box " + i).checkThisBox(0, (i<=Number(event.value)));

    }
}

1 reply

try67
Community Expert
Community Expert
July 31, 2018

Are your fields named "Check Box 1", "Check Box 2", etc.?

try67
Community Expert
Community Expert
July 31, 2018

Also, what should happen if some check-boxes are already ticked? Should it reset all of them before ticking the amount entered? Should it add the pre-ticked ones? etc.