Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

enable a textbox when check a checkbox [duplicate]

Tags:

jquery

I try using a jquery to enable a textbox when checked a checkbox. my page jquery:

<script>
$(document).ready(function(){
  $('#isNews').change(function(){
    $("#newsSource").prop("disabled",false);
});
});
</script>

html:

<label class="checkbox">
    <input type="checkbox" id="isNews" name="isNews">If you want send news:
</label>
<label for="newsSource" class="ilable">news source</label>
<input id="newsSource" name="newsSource" class="input-xlarge" disabled="" type="text" placeholder="news source">

what's the problem?

like image 346
Ehsan Avatar asked Nov 30 '22 04:11

Ehsan


2 Answers

Change to

$('#isNews').change(function(){
   $("#newsSource").prop("disabled", !$(this).is(':checked'));
});

Demo: Fiddle

like image 183
Arun P Johny Avatar answered Dec 04 '22 09:12

Arun P Johny


Apart from the error related to non-inclusion of jQuery, change your code to this:

$('#isNews').change(function () {
    $("#newsSource").prop("disabled", !this.checked);
});
like image 22
Sang Suantak Avatar answered Dec 04 '22 09:12

Sang Suantak