0

I'm trying to disable the price input field when the checkbox is ticked and remove any text. I don't know what I'm doing wrong here..

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
    $('#confP').change(function(){
        if ($('#confP').is(':checked') == true){
            $('#price').val('').prop('disabled', true);
            console.log('checked');
        } else {
            $('#price').prop('disabled', false);
            console.log('unchecked');
        }
    });
</script>

<input type="checkbox" id="confP" name="confP" value="productType"?>Configurable Product?

Price <input type="text" id="price" name="price"/>
2
  • You forgot document ready Commented Jan 11, 2014 at 20:31
  • Your code working correctly. Try this one jsfiddle.net/2s7Kr Commented Jan 11, 2014 at 21:03

3 Answers 3

1

Specify your script content in $(document).ready(function(){...});

$(document).ready(function(){
    $('#confP').change(function(){
        if ($('#confP').is(':checked') == true){
            $('#price').val('').prop('disabled', true);
            console.log('checked');
        } else {
            $('#price').prop('disabled', false);
            console.log('unchecked');
        }
    });
  });
Sign up to request clarification or add additional context in comments.

2 Comments

The example on JQuery.com for change() doesn't have this, why must I use it to make it work?
because it make sure that the page is loaded and document is ready. So $('#confP') will be able to find the element and bind the event. You can try to placing debugger; before that line and see in case of without document.ready it wont find the element #confP so it wont be able to bind the event.
1

Try this,you missed $( document ).ready(function() { Fiddle

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>

<script type="text/javascript">
$( document ).ready(function() {
    $('#confP').change(function(){
        if ($('#confP').is(':checked') == true){
            $('#price').val('').prop('disabled', true);
            console.log('checked');
        } else {
            $('#price').prop('disabled', false);
            console.log('unchecked');
        }
    });
 });
</script>

<input type="checkbox" id="confP" name="confP" value="productType"?>Configurable Product?

Price <input type="text" id="price" name="price"/>

1 Comment

The example on JQuery.com for change() doesn't have this, why must I use it to make it work?
-1

Use this to disable:

$('#price').attr('disabled', true);

And this to enable later:

$('#price').removeAttr('disabled');

Cheers.

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.