I have this
<input type="text" id="tbox" name="tbox" readonly="readonly" />
I have a button on which I do this
$('#tbox').removeAttr('readonly');
I've also tried doing this
$('#tbox').attr('readonly', false);
.. but none work..
You will need to do this when the DOM has loaded using jQuery's ready event for the document object. Here's a Working Demo
$(document).ready(function() {
$('#tbox').removeAttr('readonly');
});
or the shorthand
$(function() {
$('#tbox').removeAttr('readonly');
});
EDIT:
I just read on one of your other questions how $()
was not working but when you used jQuery()
, your code worked. That indicates that there is a conflict with the $
function, most likely due to another JavaScript framework being used on the page too that also uses the $
shorthand. You can
1- use jQuery's noConflict() to get around this. You can assign the jQuery selector function to a different alias.
2- usejQuery()
in your code in place of $()
3- wrap your jQuery code in a self-invoking anonymous function that will still allow you to use the $()
shorthand for the jQuery selector inside of it
(function($) {
$(function() {
$('#tbox').removeAttr('readonly');
});
})(jQuery);
This is an anonymous function that takes one parameter, $
and is executed immediately, passing in jQuery
as the argument for that parameter.
I know this is now Dec 2010, but I just read this post when searching on using JQuery to set and remove READONLY on a form's text box. I found and use this:
`$('#id-name').attr('readonly', true); // makes it readonly
$('#id-name').attr('readonly', false); // makes it editable`
read article on jetlogs
The difference here is that they do
<input type="text" id="tbox" name="tbox" readonly />
And then
$('#tbox').removeAttr('readonly');
should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With