0

I have a form which is disabled using <fieldset disabled="disabled">. Now I want to enable the same form. I've used javascript but it isn't enabling my form. Here is my form and javascript code:

<?php echo "<input type='button' id='disable_enable_button' onclick='enableForm()' value='Enable/Disable'>"; ?>

<form id="form1>
 <fieldset disabled="disabled">
   <table>
     <thead>
       <tr>Sr no</tr>
       <tr>Application</tr>
       <tr>Comments</tr>
     </thead>
     <tbody>
       <tr>1</tr>
       <tr>ABC</tr>
       <tr><textarea></textarea></tr>
     </tbody>
   </table>
 </fieldset>
</form>



function enableForm() {
        var form = document.getElementById("form1");
        var elements = form.elements;
        for (var i = 0, len = elements.length; i < len; ++i) {
            elements[i].readOnly = "";
        }
    }
Aman Devrath
  • 398
  • 1
  • 3
  • 21
  • `readOnly` has nothing to do with `disabled`. `disabled` is the property for the `disabled` attribute. Also, `readOnly` is a boolean property. You are setting it to a string. Don’t confuse properties with attributes. Are you sure you want to loop over `form.elements` and not `form.children`? – Sebastian Simon Apr 23 '21 at 13:10
  • you have `onclick='showform(\"enableForm\")'` where function name is `showform` but in your js code you have `enableForm()` ? – Swati Apr 23 '21 at 13:12
  • @SebastianSimon I want to loop over every form element in the form to enable it again – Aman Devrath Apr 23 '21 at 13:17
  • @Swati I've edited my question now to avoid misunderstanding – Aman Devrath Apr 23 '21 at 13:18
  • You only have added disabled to field set why do you need to loop over all elements ? why not simply use `elements[0].disabled = false;` ? . Working [code](https://jsfiddle.net/8v23tgh9/). – Swati Apr 23 '21 at 13:29
  • @Swati Thank you for the help. But I tried this in my code. It still doesn't work. https://jsfiddle.net/waynfgtz/ – Aman Devrath Apr 23 '21 at 14:09
  • 1
    In your question you didn't add disabled to other fields . check [this](https://jsfiddle.net/8q6cn5re/) updated code. – Swati Apr 23 '21 at 14:14
  • 1
    or with jquery check [this](https://jsfiddle.net/3dujc8x5/) one – Swati Apr 23 '21 at 14:30
  • @Swati this works. thank you. Can you please explain why wasn't it working? – Aman Devrath Apr 23 '21 at 14:31
  • 1
    You have added disabled to your elements not readonly so to remove them you need to use `.disabled` . Other [ways](https://stackoverflow.com/questions/11719961/javascript-remove-disabled-attribute-from-html-input) to achieve same – Swati Apr 23 '21 at 14:36

1 Answers1

0

I see a few potential issues here. I don't know how different your code here is than your actual file, but make sure that your JavaScript code is wrapped with <script></script> tags. In addition to this, I never see a call to the function enableForm(), (again, could be different in your actual code), although I see the function showForm() call a few times. Make sure this function is called, probably upon some user action (ie button click).

Feel free to comment on this with any issues/corrections, and I will help resolve them!

  • I've used script tags. and `showForm()` calls the `enableForm()`. – Aman Devrath Apr 23 '21 at 13:11
  • What exactly is the use of the PHP here? I don't see any need for it, and it seems to be increasing the complexity of the problem, rather than decreasing. With my (limited) understanding of PHP, it seems you are trying to enable th input onclick. Is this correct? I think onfocus might be better if this is true. – Finn_Lancaster Apr 23 '21 at 13:14