0

I'm trying to code a text field that is disabled when the check box is 'checked'.

Below is the code I'm using. I have no bloody idea why it's not working. I suspect it may be WordPress' fault but I'm new to Javascript so I'm hoping it's that.

<script type="text/javascript">
$('input[name=AddressCheck]').change(function(){
    if($(this).is(':checked')) {
        $("#dbltext").removeAttr('disabled');
    }
    else{
       $("#dbltext").attr('disabled','disabled');
    }    

});
</script>



<input name="AddressCheck" type="checkbox" id="AddressCheck" /><br />
<input type="text" id="dbltext" disabled/>
3
  • 1
    So you are trying to Enable the text field when you check the checkbox? or the other way around? Commented Jul 21, 2015 at 1:35
  • When the page loads the text field should be disabled. Once the checkbox is 'checked', the text field should become enabled. Commented Jul 21, 2015 at 1:38
  • Ah! see the answers below. I don't think I understood your question at first time. Sorry about that. Commented Jul 21, 2015 at 1:39

2 Answers 2

1
$(document).ready(function(){
     $('input[name=AddressCheck]').click(function(){
        if($(this).is(':checked')) { 
            $("#dbltext").removeAttr('disabled');
        } 
});
Sign up to request clarification or add additional context in comments.

Comments

0

the script runs before the browser sees the rest of the page, so the element is never found, so nothing gets bound

try

$(document).ready(init);
// or document.addEventListener('DOMContentLoaded', init);
function init(){
    $('input[name=AddressCheck]').change(function(){
    if($(this).is(':checked')) {
      $("#dbltext").removeAttr('disabled');
    }
    else{
      $("#dbltext").attr('disabled','disabled');
    }
    });
}

or move the script to the end of the body

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.