Doubt about using checked="checked" in a jquery function

Asked

Viewed 136 times

1

I have a form and wanted that when the page was loaded, if my checkbox is set, disable the "disable" form property.

In the code example below, I put checked="checked" but only works if I click to disable and click again to enable.

Lost in it.

Could you help me?

$('#meuCheckbox').click(function() {
    $('#meuTexto').attr('disabled',! this.checked)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<input type="text" disabled="disabled" id="meuTexto" /><input type="checkbox" id="meuCheckbox" checked="checked" />

1 answer

3


Use the $(document).ready(function() {}); that will define the actions when the page load is finished.

$('#meuCheckbox').click(function() {
    $('#meuTexto').attr('disabled', !this.checked)
});

$(document).ready(function() {
    $('#meuTexto').attr('disabled', !$('#meuCheckbox').is(':checked'))
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<input type="text" disabled="disabled" id="meuTexto" />
<input type="checkbox" id="meuCheckbox" checked="checked" />

Browser other questions tagged

You are not signed in. Login or sign up in order to post.