0

I can disable the textbox when the checkbox is selected, but I can't figure out how to clear the textbox if anything is in it.

  <input type="text" id="form_sc1"/>
  <input type="checkbox" id="form_setchange"/>

JQuery

                $(document).ready(function(){
                 if($('input.form_setchange').is(':checked')){
                       $("#form_sc1").attr("disabled", "disabled");
                 }
7
  • 2
    $("#form_sc1").val("") and correct your selector instead of $('input.form_setchange').is(... it should be $('input#form_setchange').is(... Commented Mar 7, 2016 at 9:04
  • doesn't work, i have added it to the existing function, and an entirely separate one. Commented Mar 7, 2016 at 9:05
  • 1
    why "Kartikeya" solution not working ! it should work Commented Mar 7, 2016 at 9:06
  • sometimes attr will not work try prop instead $("#form_sc1").prop("value", ""); Commented Mar 7, 2016 at 9:08
  • If i just set the value = ' ', it disables the textbox. Still won't remove the text. Commented Mar 7, 2016 at 9:12

5 Answers 5

1

Try this

$(document).ready(function(){
  $('input:checkbox').on('click', function(){
      if($('input:checkbox').is(':checked')){
        $('input:text').prop('disabled', 'disabled');
        $('input:text').val('');
      }
      else{
        $('input:text').prop('disabled', false);
      }
  });
});

Working JSFiddle here https://jsfiddle.net/86Lv2urz/1/

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

Comments

0

try this:

$('input:checkbox').on('click', function(){
    $('input:text').prop('disbaled', 'disabled');
    $('input:text').val('');
});

1 Comment

Same thing, disables but does not remove the text.
0

You can also try this-

$('#form_setchange').attr("value", "");

OR

$('#form_setchange').val('');

Comments

0
if($('input.form_setchange').is(':checked')){
     $("#form_sc1").prop("disabled", true).val('');
}

1 Comment

Code-only answers are discouraged. You should try to explain your answer a little more thoroughly.
0

Can you try below code once

 $('#form_setchange').change(function() {
   var $check = $(this);
   if ($check.prop('checked')) {
     $("#form_sc1").val('');
     $("#form_sc1").attr("disabled", "disabled"); 
   } else {
     $("#form_sc1").removeAttr("disabled"); 
   }
});

Hope it may helps...

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.