Reputation: 21
I disable the input/radio buttons on a form field. When I try to enable them again with a button, that works fine. http://jsfiddle.net/3Z6aQ/
$(document).ready(function() {
$(".control").toggle(
function() {
$('.target').removeAttr("disabled");
$(this).addClass("checked");
}, function() {
$('.target').attr("disabled", true);
$(this).removeClass("checked");
});
});
But now, I try to enable this input buttons again with the "enter" button, but nothing happens here...can you please help me?
$(document).ready(function() {
$(".control").toggle(
function() {
$('.target').removeAttr("disabled");
$(this).addClass("checked");
},
function() {
if (e.keyCode == 13) {
$('.target').attr("disabled", true);
$(this).removeClass("checked");
return false;
}
});
});
and that is what I tried in the last hour...but I have some problems with the code
$(document).ready(function() {
$(".control").toggle(
function() {
$('.target').removeAttr("disabled");
$(this).addClass("checked");
},
$('input').keypress(function(event) {
var keycode = (event.keyCode ? event.keyCode : event.which);
if (keycode == '13') {
$('.target').removeAttr("disabled");
$(this).addClass("checked");
alert('You pressed a "enter" key in textbox');
}
});
});
});
Upvotes: 1
Views: 15342
Reputation: 24375
Is it very unclear on what you are after, so it would be good if you could provide us with some more information.
But form what I gathered, this is what you are after:
$(document).ready(function () {
$(".control").toggle(
function () {
$('.target').removeAttr("disabled");
$(this).addClass("checked");
}, function () {
$('.target').attr("disabled", true);
$(this).removeClass("checked");
});
});
Also, make sure that you are using jQuery 1.7 since that is what is within your Fiddle.
Upvotes: 1
Reputation: 36784
I went back to your first fiddle, and then added a keypress event for your input. Since your only ever going to be able to trigger a keypress on the input when it's enabled, you can trigger a click on your 'checkbox' - which in turn, will disable the input:
$(document).ready(function () {
$(".control").toggle(
function () {
$('.target').removeAttr("disabled");
$(this).addClass("checked");
}, function () {
$('.target').attr("disabled", true);
$(this).removeClass("checked");
});
$('input').keypress(function (e) {
if (e.which == 13) {
$('.control').click();
}
});
});
Upvotes: 0