Disable button by clearing checkbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tsubasa
    New Member
    • Aug 2008
    • 64

    Disable button by clearing checkbox

    I have a form where a user can select items via a check box. Once an item is checked it will enable a button to appear so that the user can add the items to a shopping cart. I have the first part of the JavaScript working which allows the button to appear if the checkbox is true.

    The problem that I am having is when I decide to deselect the last remaining box. It does not disable the add button. The first part works, the second part does nothing. Also, there are no JavaScript errors.

    -Tsu

    Code:
    function boxChecked(field)
    {
      var 1 = 0;
      if(field[i].checked = true)
      {
       var x=document.getElementById("mybutton")
       x.disable=false;
      }
      else
      if(field[i].checked = false)
      {
      var x=document.getElementById("mybutton")
      x.disable=true;
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    that’s because = is the assignment operator.

    tip: reverse variable and value.
    Code:
    if (box.checked = false) // no error
    
    if (false = box.checked) // error: cannot assign to a constant

    Comment

    • tsubasa
      New Member
      • Aug 2008
      • 64

      #3
      Hello Dormilich,

      Although this does not produce an error, it does not work. The same remains even with your recommendation. The first part works, but the 2nd part does nothing. Thanks for replying anyhow.

      -Tsu

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        er, my code was not meant as a working demonstration. it is an example to demonstrate the problem.

        you are just mistaking the assignment operator for the comparison operator. the given code shows a possibility to write code in a way that you can’t mistake one for the other.

        Comment

        • tsubasa
          New Member
          • Aug 2008
          • 64

          #5
          Understood,

          Thanks!

          Comment

          Working...