Hassan Z
Hassan Z

Reputation: 446

How to enable a disabled text field?

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.

Upvotes: 16

Views: 91689

Answers (5)

amol15
amol15

Reputation: 11

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');

Upvotes: 1

Bins Jose
Bins Jose

Reputation: 81

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

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

Upvotes: 8

zajc3w
zajc3w

Reputation: 211

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");

Upvotes: 11

Purag
Purag

Reputation: 17061

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().

Upvotes: 2

Adam Rackis
Adam Rackis

Reputation: 83356

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);

Upvotes: 19

Related Questions