0

my first javascript is not working, hope you can help.

I am trying to make a checkbox on one form check a checkbox on another form. This is based on this link: check/uncheck checkbox based on another checkbox

Here is my simplified code

<formname="A"
action="WebPage.php" method="POST">
<input type="checkbox" name="Aa" onchange="update()" value="Aa"/>Aa
<script type="text/javascript">
    function update(){
        var original = document.getElementById('Aa');
        var other = document.getElementById('Ba');
        original.checked = other.checked;
    }
</script>    
</form>
<formname="B"
action="WebPage.php" method="POST">
<input type="checkbox" name="Ba" value="Ba"/>Ba
</form>

This is not working. Checking Aa does nothing to Ba.

In case I am going down the wrong path, if this can work, then I will change it so checking Aa unchecks Ba, Bb, Bc, and other checkboxes on form B. Then I want to add a function on form B so that checking any one or all checkbox(es) there will cause Aa to be unchecked.

I am at the very beginning of website design, so I appreciate your advice on the best direction to go.

Thanks, Dan

1
  • You are trying to access elements by id.So add id in both checkbox. e.g., <input type="checkbox" id="Aa" name="Aa"> Commented Jun 23, 2015 at 4:25

3 Answers 3

1

There is no id property for the checkbox elements, also you need to change the assignment to other.checked = original.checked;

<form name="A"
      action="WebPage.php" method="POST">
  <input type="checkbox" name="Aa" id="Aa" onchange="update()" value="Aa"/>Aa
  <script type="text/javascript">
    function update(){
      var original = document.getElementById('Aa');
      var other = document.getElementById('Ba');
      other.checked = original.checked;
    }
  </script>    
</form>
<form name="B"
      action="WebPage.php" method="POST">
  <input type="checkbox" name="Ba" id="Ba" value="Ba"/>Ba
</form>

Sign up to request clarification or add additional context in comments.

Comments

0
function update(){

    var original = document.getElementById("Aa");
    var other= document.getElementById("Ba");

    if (original .checked == true )
      other.checked = true;
    else
      other.checked = false;
}

Comments

0
    <form name="A" action="WebPage.php" method="POST"> 
    <input type="checkbox" name="Aa" id="Aa" onchange="update()" value="Aa"/>Aa
    <script type="text/javascript">
    function update(){

    var original = document.getElementById('Aa').checked ;
    var other= document.getElementById('Ba').checked ;

    if (original == true ){
      other = true;
    }else{
      other = false;}
   }
    </script>    
</form>
<form name="B"action="WebPage.php" method="POST">
  <input type="checkbox" name="Ba" id="Ba" value="Ba"/>Ba
</form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.