0

I have a check box which will change the value of a textfield to 1 when checked and change it back to zero when unchecked, how can I do this? I want it in javascript.

4 Answers 4

1
<input type="checkbox" name="foo" onclick="document.getElementById('idtextfield').value = +this.checked;" />
Sign up to request clarification or add additional context in comments.

Comments

1

You can do like this:

  var chk = document.getElementById('checkbox_id');
  var txt = document.getElementById('textbox_id');

  chk.onclick = function(){
    if (this.checked === true){
      txt.value = '1';
    }
    else{
      txt.value = '0';
    }
  };

Comments

0
window.onload = function() {
    var checkBox = document.getElementById('idofcheck');
    if (checkBox == null) {
        return;
    }

    checkBox.onclick = function() {
        var textField = document.getElementByd('ifoftextfield');
        if (textField == null) {
            return;
        }

        if (this.checked) {
            textField.value = '1';    
        } else {
            textField.value = '0';
        }
    };
};

Comments

0
  if(document.getElementById("checkbox1").checked)
  {
    document.getElementById('textbox1').value=1;
  }

  else
  {
    document.getElementById('textbox1').value='';
  }

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.