views:

138

answers:

3

Aparently a disabled input is not handled by any event (am I wrong ?)

Is there a way to work arround this problem ?

<input type="text" disabled="disabled" name="test" value="test" />

$(':input').click(function () {
  $(this).removeAttr('disabled');
})

Here I need to click on the input to enable it. But if I don't activate it, the input should not be posted

+5  A: 

Disabled elements don't fire mouse events at all. I can't think of a better solution, but you could place an element in front of the input, and catch the click on that element. Here's an example of what I mean, it's not valid HTML though and it will need tweaking a bit:

<div style="display:inline-block; position:relative;">
  <input type="text" disabled />
  <div style="position:absolute; left:0; right:0; top:0; bottom:0;"></div>
</div>​

jq:

$("div > div").click(function (evt) {
    $(this).hide().prev("input[disabled]").attr("disabled", false).focus();
});​

Example: http://jsfiddle.net/RXqAm/

Andy E
+1 for jsfiddle example..
Krunal
Small thing: if you're using the `disabled` attribute with no value, that implies HTML rather than XHTML, in which case the closing slash is unnecessary.
Tim Down
@Tim: indeed it is unnecessary, but it's still valid HTML. It's just a force of habit really and I feel like it looks better.
Andy E
Thx Andy, this is quite smart. Isn't there simpler ? Do you know why do desabled inputs are not handleable ?
Glide
@Glide: I guess the UA's just followed suit on this one, the W3C DOM spec doesn't specifically mention anything about not firing mouse events on a disabled element (as far as I can tell). I only wish I could think of an easier way, maybe somebody else will.
Andy E
Ok thx for the info.
Glide
+2  A: 

hm.. maybe you could make the field readonly and on submit disable all readonly fields

$(".myform").submit(function(e) {
    $("input[readonly]", this).attr("disabled", true);
});

and the input (+ script) sould be

<input type="text" readonly="readonly" name="test" value="test" />

$('input[readonly]').click(function () {
    $(this).removeAttr('readonly');
})
Tokimon
+1, this is a decent alternative suggestion. The only downside is that the input box will not take on the disabled styling, which varies between browsers so it would be hard to make it look consistent with the user's expectations of a disabled input.
Andy E
Also interesting indeed. Thx
Glide
true. Perhaps this could be worked around with CSS?But yes it would not have the same look as normal diabled input fields
Tokimon