Nathan
Nathan

Reputation: 5372

jQuery selector - input field

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.

Upvotes: 0

Views: 450

Answers (2)

Yi Jiang
Yi Jiang

Reputation: 50115

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.

Upvotes: 0

leeny
leeny

Reputation: 626

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

Upvotes: 9

Related Questions