Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove disabled attribute onClick of disabled form field

Tags:

javascript

I have a form field that starts out disabled and has an onClick to enable it. The onClick doesn't fire (at least in FF) nor does a simple alert(1);.

The hacky version is to show a fake form field in its place that "looks" like it's disabled (grayed out style) and onClick, hide it and show the correct field enabled, but that's ugly.

Example Code

This works:

<input type="text" id="date_end" value="blah" onClick="this.disabled=true;">

This works:

<label for="date_end_off" onClick="document.getElementById('date_end').disabled=false">Test</label>
<input type="text" id="date_end" value="blah" onClick="alert(1);" disabled>

This fails:

<input type="text" id="date_end" value="blah" onClick="alert(1);" disabled>

This fails:

<input type="text" id="date_end" value="blah" onClick="document.getElementById('date_end').disabled=false" disabled>
like image 886
iamgoat Avatar asked May 28 '09 14:05

iamgoat


People also ask

How do I remove disabled attributes?

To remove the disabled attribute, select the element and call the removeAttribute() method on it, passing it disabled as a parameter, e.g. btn. removeAttribute('disabled') . The removeAttribute method will remove the disabled attribute from the element.

How do I remove input field Disabled?

To remove disabled attribute using jQuery, use the removeAttr() method. You need to first remove the property using the prop() method. It will set the underlying Boolean value to false.

How do I disable a form tag?

Disabling all form elements HTML form elements have an attribute called disabled that can be set using javascript. If you are setting it in HTML you can use disabled="disabled" but if you are using javascript you can simply set the property to true or false .

How do I disable a field in HTML?

Create an HTML table using the <table> element. Now add the <form> element within this table. Next, we will create form fields. We add the required form fields to the form using the <tr> element that is used to add rows to a table.


2 Answers

Use readonly instead of disabled

For checkboxes at least, this makes them look disabled but behave normally (tested on Google Chrome). You'll have to catch the click and prevent the default action of the event as appropriate.

like image 182
EoghanM Avatar answered Oct 07 '22 11:10

EoghanM


I recently had a very similar problem and solved it by placing the input in a div and moving the onClick to the div.

<div onClick="myEnableFunction('date_end');">
<input type="text" id="date_end" value="blah" disabled>
</div>
like image 22
Mat Barnett Avatar answered Oct 07 '22 11:10

Mat Barnett