user2168066
user2168066

Reputation: 635

clear and disable textbox when checkbox checked

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");
                 }

Upvotes: 0

Views: 3398

Answers (5)

user1846747
user1846747

Reputation:

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/

Upvotes: 1

getty
getty

Reputation: 135

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...

Upvotes: 0

rmondesilva
rmondesilva

Reputation: 1792

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

Upvotes: 0

Vipul
Vipul

Reputation: 941

You can also try this-

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

OR

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

Upvotes: 0

Gaurav Aggarwal
Gaurav Aggarwal

Reputation: 10177

try this:

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

Upvotes: 0

Related Questions