Skip to main content
Staceypayne89
Participant
September 16, 2020
Question

If and checkboxes - Auto checking a checkbox once 2 others are checked

  • September 16, 2020
  • 1 reply
  • 445 views

The below statment will not check my checkbox3 once checkbox1 && checkbox2 are checked it will only work with the || (or) statement, what am I doing wroung? 

 

Document Level Control

 

function cbControl() {

// Get a reference to each check box

var f1 = getField("checkbox1");

var f2 = getField("checkbox2");

var f3 = getField("checkbox3");

 

// Check cb3 if cb1 and cb2 was selected

if (event.target === f1 && event.target === f2)

{ f3.value = "Yes"; return; }

}

 

Mouse Up Event on each checkbox

cbControl();

This topic has been closed for replies.

1 reply

BarlaeDC
Community Expert
Community Expert
September 16, 2020

Hi,

 

event.target cannot be equal to both f1 and f2, it is the target of the event ( so it will be either f1 or f2).

You need to change the code so that if event.target is f1, you use f2.value to work out if F2 is also checked.

And the same the other way round when event.target is f2.

 

Pleaes note: as this is a checkbox, there is no guarentee that the mouse up is for the checkbox being checked , it may be it being unchecked so you might want to check that value to make sure it is selected ( you can use event.value for the one that is being clicked).

 

hope that helps

 

Malcolm

Staceypayne89
Participant
September 16, 2020

I am pretty new to Javascript... and not quite understanding exactly where to change the code? Would you be able to show me an example? 

 

Thanks in advance.