jQuery: Disabled and ReadOnly Inputs

Posted by Jetlogs @ 12:22 am
Category: jQuery,Web Development

Ever wonder how to disable another input element through the use of another control? Here is a simple tutorial on how to toggle the disabled attribute of an input element.

For the first demo, we will toggle an input’s disabled status through the use of another control. For this example, we will give our target input an id of target, while our control, in this case a link, we will give it an id of control:

Target text field: <input type="text" id="target" /><br />
<a href="#" id="control" />Disable target</a>

Now to toggle the target‘s disabled status, it is actually quite simple in jQuery. Using the .attr() attribute we can disable the target. Then using the .removeAttr(), we can remove the disabled status. Here is how the javascript code would look like:

$("#control").toggle(
function ()
{
	$('#target').attr("disabled", true);
},
function ()
{
	$('#target').removeAttr("disabled");
});

Here is a demo on how to toggle the disabled status of an input:

Target text field:
Disable target

Although the first method is handy, there are times when we really don’t need a control input. What if we want to enable an input textbox just by clicking on it? Unfortunately, when an input element is disabled, it would no longer register events such as focus or click. The good news is that there is an alternative. Instead of using the disabled attribute, we use the readonly attribute instead. Here is a sample javascript code on making a readonly input enabled using its click event:

$('#readonly').click(
function()
{
	if ($('#readonly').attr("readonly") == true)
	{
		$('#readonly').val('');
		$('#readonly').removeAttr("readonly");
	}
});

Here is the demo of how to change the readonly attribute

This text field will no longer be read-only upon clicking:

If you want to download the source code for this tutorial,
just click on the link below:

Download Source Code


34 Comments »

34 Comments to “jQuery: Disabled and ReadOnly Inputs”

  1. RaMa-NoOb
    1

    wow, are you reading my mind? was gonna ask you about this a few days ago :) ) cheers man! you’re my fountain of knowledge hahaha

  2. Paras Jain
    2

    This was of great help. Thank you.

  3. Mike
    3

    Readonly still sends the value in the forms collection during request, though. Disabled does not…these could be important distinctions for server-side code handling.

  4. Jetlogs
    4
    Author Comment

    @Mike: Yes, disabled fields don’t get sent to the back-end while readonly fields do. So it is very important to know which you would use depending on the situation.

  5. prasasti
    5

    jetlogs. Tanks.4 u help.

  6. yAnTar
    6

    $(‘#target’).attr(“disabled”, true); = $(‘#target’).disable();
    $(‘#target’).removeAttr(“disabled”); = $(‘#target’).disable(false);

  7. yAnTar
    7

    Sorry, and this function

    $.fn.disable = function() {
    return this.each(function() {
    if(typeof this.disabled != “undefined”) this.disabled = true;
    });
    }

  8. Mike Rundle
    8

    Thanks for posting this! I was trying to flip the disabled attribute to “false” but that wasn’t doing anything (like I suspected it wouldn’t!) What a great and simple solution to just remove it. Duh! Thanks so much!

  9. Alexandre Broggio
    9

    Help much worth the post

  10. Mark
    10

    This doesn’t seem to work in IE 8. Thoughts? I’m new to JS/jQuery

  11. Mark
    11

    Nevermind, IE 8 doesn’t give visual cues to a disabled field.

  12. Vitali
    12

    If you want disable input element through the use checkbox you can use this code.

    $(document).ready(function()
    {
    $(“#checkbox1″).click(function() {
    // If checked
    if ($(“#checkbox1″).is(“:checked”)) {
    $(‘#input1′).removeAttr(“disabled”);
    } else {
    $(‘#input1′).attr(“disabled”, true);
    }
    });
    });

  13. Thomas B. Higgins
    13

    Following Vitali’s example, if you want to disable an input element through the use of a checkbox in a situation where data from a database record is being changed, you can use this code:

    $(document).ready(function() {
    // Initialize to match existing (database) status
    // If checked:
    if ($(“#checkbox1″).is(“:checked”)) {
    $(‘#input1′).removeAttr(“disabled”);
    }
    // If unchecked:
    else {
    $(‘#input1′).attr(“value”, ”);
    $(‘#input1′).attr(“disabled”, true);
    }
    // On click of “checkbox1″ change value and
    // disabled status of selected field
    $(“#checkbox1″).click(function() {
    // If checked:
    if ($(“#checkbox1″).is(“:checked”)) {
    $(‘#input1′).removeAttr(“disabled”);
    }
    // If unchecked:
    else {
    $(‘#input1′).attr(“value”, ”);
    $(‘#input1′).attr(“disabled”, true);
    }
    });
    });

    Note that the value of the disabled input element is set to an empty string just prior to being disabled for neatness’ sake and to prevent possible confusion on the part of the user caused by a “trapped” value.

  14. pete castillo
    14

    To disable in IE, add change the following css setting so that you can visually distinguish a disabled field. You may only want to do this for IE…(as mark previously mentioned IE doesn’t give visual cues when a field is disabled):

    if (bEnable) {
    $(‘#ctrl_id’).attr(‘disabled’, true);
    $(‘#ctrl_id’).css(‘background-color’, ‘#e0e0e0′);
    } else {
    $(‘#ctrl_id’).removeAttr(‘disabled’);
    $(‘#ctrl_id’).css(‘background-color’, ‘#ffffff’);
    }

  15. Pingback:
    15
    JQuery – Disable form input « MarkRoper's Blog

    [...] $('#readonly').click(  function()  {      if ($('#readonly').attr("readonly") == true)      {          $('#readonly').val('');          $('#readonly').removeAttr("readonly");      }  }); http://jetlogs.org/2007/12/16/jquery-disabled-and-readonly-inputs/ [...]

  16. Ranju
    16

    Hi,

    I am able to disable my datalist using this code in IE, but its not working in Mozilla. Can anybody help me plz….

    Thanks

    Ranju

  17. restroika
    17

    – thanks —
    anda membaca pikiranku..

  18. Mark
    18

    It’s very nice article
    Thank you very much.
    It work in firefox as well as IE

  19. ramzee
    19

    Thank You man. Like quick manual to me.

  20. silver
    20

    great Like quick manual to me.

  21. Michel
    21

    Seems select does not have a readonly attribute so more code is needed for that

  22. km
    22

    merccccccccccccccci

  23. Virendra
    23

    Hi,

    There is another way of making the textbox readonly. Read this link.

    http://jquerybyexample.blogspot.com/2010/06/make-readonly-textbox-using-jquery.html

    Thanks,
    Virendra

  24. Vijay
    24

    Please find below the link to enable and disable any element using jQuery.

    http://jquerybyexample.blogspot.com/2010/06/disableenable-element-using-jquery.html

    Thanks,
    Vijay

  25. vipul
    25

    Thanks a lot ..

  26. raghu
    26

    hi this raghu

  27. TheCrowned
    27

    Thanks man, this helped!

  28. denny
    28

    Thank, I’ve already tested on my script
    and it works

  29. khadija
    29

    Its very nice thanks for this guideline

  30. khaleja
    30

    Thanks !

  31. Doug Estep
    31

    I created a widget that can completely disable or present a read-only view of the content on your page. It disables all buttons, anchors, removes all click events, etc., and can re-enable them all back again. It even supports all jQuery UI widgets as well. I created it for an application I wrote at work. You’re free to use it.

    Check it out at ( http://www.dougestep.com/dme/jquery-disabler-widget ).

  32. web dev
    32

    This doesnt work in Internet Explorer. after readonly is removed, and the cursor is focused on the textbox, the user cannot enter text.. they must give focus a second time. :-(

  33. disabled singles dating
    33

    What’s up it’s me, I am also visiting this website daily,
    this site is truly pleasant and the people are in fact sharing
    pleasant thoughts.

  34. Ericka Phuma
    34

    CSS is designed primarily to enable the separation of document content (written in HTML or a similar markup language) from document presentation, including elements such as the layout, colors, and fonts.This separation can improve content accessibility, provide more flexibility and control in the specification of presentation characteristics, enable multiple pages to share formatting, and reduce complexity and repetition in the structural content (such as by allowing for tableless web design). `’

    Warm regards
    <http://www.foodsupplementdigest.com

RSS Comment Feed Comments RSS |trackback TrackBack URI

Leave a comment

  • Archives

  • Donations

  • Social Bookmarks

  • Jetlogs.org
    Some Rights Reserved 2007
    Creative Commons License