4

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?

Ehsan
  • 2,273
  • 8
  • 36
  • 70

6 Answers6

14

Change to

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

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
4

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

$('#isNews').change(function () {
    $("#newsSource").prop("disabled", !this.checked);
});
Sang Suantak
  • 5,213
  • 1
  • 27
  • 46
2

I don't speak Arabic, but you're including jQuery after the rest of all of your scripts. You need to include jQuery first.

You're getting "Undefined variable $" in the console. Always pay attention to the console.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
0

the error you have there is

$ is not defined

so you probabaly havn't loaded jquery.. :)

add this in your <head> tag

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
bipen
  • 36,319
  • 9
  • 49
  • 62
0

Here is your correct code

<script>
$(document).ready(function(){
  $('#isNews').click(function(){
    if($(this).is(":checked"))
       $("#newsSource").removeAttr("disabled");
    else
       $("#newsSource").atte("disabled" , "disabled");
});
});
</script>
Ali Foroughi
  • 4,540
  • 7
  • 42
  • 66
0

I opened the link given by you. in your page source the problem is you are including jquery after the the jquery code. Try adding it before the document.ready code.