1

I want to clear the value of an input field (textbox) when the user unchecks a checkbox.

When the user types in the field, I want to check the checkbox.

Edit: Funny how you get downvotes for doing exactly what StackOverflow recommends: https://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/

12
  • 2
    What have you tried? What are the problems that you encountered during solving this task and what can we help in overcoming them? Having >3k reputation, you cannot be considered a rookie, so you should know what makes an appropriate question on SO. Commented Dec 5, 2013 at 11:19
  • 2
    There is no problem with answering your own question at all, you misunderstand. The problem is asking a question that does not meet the quality requirements of SO. If a rookie asked this same question, it would already be mass-downvoted and closed - for a reason. Answering it yourself makes absolutely no difference. Commented Dec 5, 2013 at 11:47
  • 1
    Next time you can just edit the damn question instead of making smartass comments. Really, do it. Make the question better. Perhaps you can copy paste my answer into the question, remove a line or introduce a bug in some other way. I'm sure that will make this question more usable for future visitors. Commented Dec 5, 2013 at 11:56
  • 1
    Congratulations on your comment :). Commented Dec 5, 2013 at 12:23
  • 1
    @kapa I agree with Thomas Stock. SO is the perfect avenue for this type of question. Many future visitors will find value in the answers given. Commented Dec 5, 2013 at 15:57

3 Answers 3

1

With this HTML:

<input id="thecheckbox" type="checkbox">
<input id="theinput" type="text">

You can use this javascript:

$("#theinput").keyup(function () {
    if ($(this).val() == "") {
        $("#thecheckbox").prop("checked", false);
    } else {
        $("#thecheckbox").prop("checked", true);
    }
});

$("#thecheckbox").change(function () {
    if (!$(this).is(':checked')) {
        $("#theinput").val("");
    }
});

It works when using the keyboard for tabbing and/or checking the checkbox.

http://jsfiddle.net/B39Yq/

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

1 Comment

Within the eventhandler for the checkbox, instead of $(this).is(':checked') you can simply use this.checked)
1

HTML

<input id="text"/>
<input type="checkbox" id="check"/>

Javascript

$("#text").keyup(function(){
    $("#check").get(0).checked = !!$(this).val().trim();
});

$("#check").click(function(){
    (this.checked) ? "":$("#text").val("");
});

JS Fiddle: http://jsfiddle.net/p5u64/1/

1 Comment

Would it not be a good idea to store the value of the checkbox so that you can bring back the value if you've typed something in the text-field and then uncheck and re-check the checkbox?
1
function onKeyUp(event) {
  var target = event.target;
  if (target.nodeName && target.nodeName.toLowerCase() === "input") {
    if (target.type && target.type == "text") {
      changeCheckboxes(this, target.value)
    }
  }
}

function onChange(event) {
  var target = event.target;
  if (target.nodeName && target.nodeName.toLowerCase() === "input") {
    if (target.type && target.type == "checkbox" && !this.checked) {
      clearTextfields(this);
    }
  }
}

function clearTextfields(form) {
  var elements = form.elements;
  for (i = elements.length; i--;) {
    if (elements[i].type === "text") {
      elements[i].value = "";
    }
  }
}

function changeCheckboxes(form, check) {
  var elements = form.elements;
  for (i = elements.length; i--;) {
    if (elements[i].type === "checkbox") {
      elements[i].checked = check;
    }
  }
}

var form = document.forms[0];
form.addEventListener("keyup", onKeyUp);
form.addEventListener("change", onChange);
form.addEventListener("click", onChange);

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.