How to enable a disabled text field?
Asked Answered
C

5

16

I wanna know how do I enable a disabled form text field on submit. Also I wanna make sure if user goes back to form or click reset field will show again as disabled.

I tried to use

document.pizza.field07.disabled = false ;

It does disables the field, by clicking reset or hitting back button still keeps it enable.

Please guide.

Constantinople answered 13/12, 2011 at 4:5 Comment(0)
H
19

To access this element in a more standard way, use document.getElementById with setAttribute

document.getElementById("field07").setAttribute("disabled", false);

EDIT

Based on your comment, it looks like field07 is a name, not an id. As such, this should be what you want:

var allfield7s = document.getElementsByName("field07");
for (var i = 0; i < allfield7s.length; i++)
    allfield7s[i].setAttribute("disabled", false);
Hello answered 13/12, 2011 at 4:7 Comment(7)
I tried 'this:document.getElementsByName("field07").setAttribute("disabled", false);' but its not working.... field07 is name...Constantinople
Don't bother with buggy setAttribute, just set the property directly: allfield7s[i].disabled = false;Inexperienced
@Rob - I thought setAttribute was widely supported?Hello
@AdamRackis: It is widely supported, but the property is simpler, just as standard, better supported and more readable.Rockrose
@AdamRackis—it's widely supported, but implementations are inconsistent. Google "javascript setattribute issues IE".Inexperienced
Thanks @TimDown - didn't realize setAttribute wasn't perfectly supportedHello
On my Browser Firefox setattribute didn't work. So I used @Inexperienced 's solution.Depurate
B
11

That is the only working solution for Me:

var allfield7s = document.getElementsByName("field07");
for (var i = 0; i < allfield7s.length; i++)
    allfield7s[i].removeAttribute("disabled");
Bronson answered 21/12, 2013 at 16:11 Comment(1)
This is the best answer. Setting disabled to false per the accepted answer will just change the attribute to 'disabled = false' and the element will remain disabled.Module
R
8

You can enable a disabled html control with the following JavaScript code.

document.getElementById('elementId').removeAttribute('disabled');

Rudimentary answered 7/7, 2014 at 14:8 Comment(1)
Could you please add a link to the relevant documentation ?Allie
T
2

You can also do this with jQuery:

$(function(){
    $("[name='field07']").prop("disabled", false);
});

We simply select all the elements where the name attribute is field07 (using name because you said so in the comments of @AdamRackis's answer) and set its disabled property to false.

More about prop().

Treaty answered 13/12, 2011 at 4:43 Comment(1)
While this works, I downvoted it because using a third party library to change the attribute value of an element is not needed.Tartan
R
1

You can enable a disabled html control(like, input, textarea, button,...) with the help following code.

To disable:

document.getElementById("id_name").setAttribute("disabled", true);

To enable:

document.getElementById('id_name').removeAttribute('disabled');
Rm answered 28/7, 2020 at 10:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.