12

I have a form with some inputs disabled. I want these inputs to be enabled when double clicked. Unfortunately, JS events seem not to fire on disabled inputs.

<input type="text" value="asdf" disabled="disabled" ondblclick="this.disabled=false;">​

Is there a way around this limitation?

Or am I just going to have to wrap any affected input in a span to house the event?

http://jsfiddle.net/vhktx/

Umbrella
  • 4,733
  • 2
  • 22
  • 31

4 Answers4

23

ondblclick does not get fired on a disabled element, you should mark it as readonly:

<input type="text" value="asdf" readonly="true" ondblclick="this.readOnly='';">

http://jsfiddle.net/vhktx/4/

JonWarnerNet
  • 1,112
  • 9
  • 19
  • Yes, this is a preferred solution. Thank you. – Umbrella Jul 03 '12 at 23:58
  • 4
    You can also add `onblur="this.readOnly='true';"` to make it return to its read-only state when clicked outside the input. And use the `:read-only` CSS tag to style the element while it's read-only vs editable. – johnozbay Feb 10 '17 at 14:00
5

You will have to wrap the element in a container since the the event will not fire on disabled elements.

jsFiddle

<div ondblclick="document.getElementById('test').disabled=false;">
    <input type="text" id="test" value="asdf" disabled="disabled"></div>
Gabe
  • 49,577
  • 28
  • 142
  • 181
2

The answer that is marked correct does not revert the input back to needing another double-click to edit again.

Add this onchange function to do so:

onchange="this.readOnly='true';"

The whole thing should be:

onchange="this.readOnly='true';" readonly="true" ondblclick="this.readOnly='';"
Syed M. Sannan
  • 1,061
  • 2
  • 9
  • 28
Emani
  • 21
  • 1
-1

this will do! works for me, As 'disabled' and 'readonly' appears almost the same(at least in bootstrap). inline scripts don't work with the disabled attr.

<input type="text" ondblclick="this.readOnly=false;" readonly/>