0

I'd like to enable the textbox when it is clicked. However, when I click the textbox, nothing happens. I believe it is a problem with the jQuery selector. Why isn't this working?

<script> $(document).ready(function() { $(':input').click(function() { $(this).removeAttr('disabled'); }); }); </script> <input type="text" value="123" disabled="disabled" /> 

Note: I tried both $('input') and $(':input') to select the textfield. Neither worked.

3
  • 1
    Do you know if the function is being called? Try putting an alert statement in the function to see if it's being called in the first place. Commented Jan 11, 2011 at 7:17
  • 1
    This is a duplicate of Remove disabled attribute onClick of disabled form field. Long story short, disabled elements do not fire click events. Commented Jan 11, 2011 at 7:19
  • Just curious, can you explain the use case for such behavior? Commented Jan 11, 2011 at 7:19

2 Answers 2

8

A disabled input isn't going to fire events. Try changing from disabled to readonly.

Sign up to request clarification or add additional context in comments.

1 Comment

Keep in mind that this only works for type="text" and type="password"
0

It has nothing to do with the selector you're using, but rather because, since the input element is disabled, the events for the input will not fire - see: http://www.jsfiddle.net/DvZDh/

<input type="text" value="123" disabled="disabled" /> <input type="text" value="123" /> 

The code works on the second input element, but not the first. A simple solution would probably be to use CSS to simulate the disabled state instead.

1 Comment

Thank you for the link, it's very useful. That's a great website.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.