ruturaj
ruturaj

Reputation: 3453

jQuery readonly attribute problems

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

Upvotes: 7

Views: 40494

Answers (4)

Natrium
Natrium

Reputation: 31174

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.

Upvotes: 2

Russ Cam
Russ Cam

Reputation: 125488

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.

Upvotes: 14

Shivam Srivastava
Shivam Srivastava

Reputation: 4596

    <input type="submit"  id="btn1" onclick="setTimeout(disablefunction, 1);">

    add function of java script
        <script type="text/javascript" >


        function disablefunction() 
        {
            $('#btn1').attr('disabled',true)
        }


        </script>

I am sure this will work 100%

Upvotes: 0

JohnC
JohnC

Reputation: 21

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`

Upvotes: 2

Related Questions