Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable readonly to text box onclicking the button

I have used "readonly" attribute to the textbox which makes the textbox non editable and may i know how to disable the readonly attribute on clicking the button. can we do this by using any script ?

<input type="text" name="" value="" readonly="readonly"/><br/>
<input type="submit" name="" value="update" onclick="" />
like image 335
Anand Avatar asked Feb 06 '14 16:02

Anand


People also ask

How do I enable readonly and disable?

Some read-only files can be changed to allow for edits by removing the read-only attribute in the file properties. Right-click the file and select Properties. Uncheck the box for Read-only and click OK.

How do I toggle readonly in HTML?

click(function(){ // when the input is clicked, it looks to the previous input element // that has a `required` attribute, and sets its `readonly` property, // the `r` is the current readonly state (true or false) $(this). prev('input[required]').

How do I change a JavaScript property from read only?

Use setAttribute() Method to add the readonly attribute to the form input field using JavaScript. setAttribute() Method: This method adds the defined attribute to an element, and gives it the defined value. If the specified attribute already present, then the value is being set or changed.


2 Answers

You can do two things:

  • Remove the readonly (case insensitive) attribute, using removeAttribute
  • Set the readOnly (case sensitive) property to false

HTML:

<input id="myInput"  type="text"  readonly="readonly" /><br />
<input id="myButton" type="submit" value="update" />

JS (option 1): [Demo]

document.getElementById('myButton').onclick = function() {
    document.getElementById('myInput').removeAttribute('readonly');
};

JS (option 2): [Demo]

document.getElementById('myButton').onclick = function() {
    document.getElementById('myInput').readOnly = false;
};
like image 180
Oriol Avatar answered Oct 20 '22 12:10

Oriol


Well, to be simple! You can use jQuery to remove this attribute (jQuery would be simple, you can do this using JS too). Here is the code for that:

$('input[type=submit]').click(function () {
  $('input[type=text]').removeAttr('readonly');
})

https://api.jquery.com/removeAttr/

like image 40
Afzaal Ahmad Zeeshan Avatar answered Oct 20 '22 10:10

Afzaal Ahmad Zeeshan