Reputation: 137
I want to change text box text depending if checkbox is checked or not. So if the user checks the checkbox, the text box shows some text and if the user unchecked the checkbox, it shows some different text.
HTML:
<input id="check" type="checkbox" />
<input id="txt" type="text" value="aaaa" />
jQuery:
$('input[type=checkbox]').each(function () {
if (!$('input[type=checkbox]').is(':checked')) {
$('#txt').val('checked');
}
else{
$('#txt').val('unchecked');
}
});
Upvotes: 2
Views: 8325
Reputation: 2370
Try binding to the click event:
$('input[type=checkbox]').click(function () {
if ($('input[type=checkbox]').is(':checked')) {
$('#txt').val('checked');
}
else{
$('#txt').val('unchecked');
}
});
Upvotes: 3
Reputation:
I think the best solution would be to use .change()
and this.checked
like so:
<input id="check" type="checkbox" />
<input id="txt" type="text" value="unchecked" />
$('#check').change(function(){
var checkChange = this.checked ? 'checked' : 'unchecked';
$('#txt').val(checkChange);
});
Upvotes: 0