After Adding "readonly" attribute on text box not able to remove it in one event

Posted by Sreedhar K on Stack Overflow See other posts from Stack Overflow or by Sreedhar K
Published on 2010-12-21T07:04:02Z Indexed on 2010/12/21 8:54 UTC
Read the original article Hit count: 343

Steps to repro
USE Internet Explorer

  1. Check unlimited check box
  2. Click on text box (It will remove tick/check from check box)
  3. Try to enter text in text box

We cannot enter in the text box
4. Click again on the text box. Now we will be able to enter text in the text box

We tried by
1. Making attribute readOnly to flase i.e. $('#myinput').attr('readOnly', false);
2. Calling $('#myinput').click();

Below is the HTML code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Make input read only</title>
    <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.4.min.js"></script>
</head>
<body>
    <input id="myinput" type="text" />
    <input id="mycheck" type="checkbox" />
    <script type="text/javascript">
        /*oncheck box click*/
        $('#mycheck').click(function () {
            if ($(this).attr('checked')) {
                $('#myinput').attr('readOnly', 'readOnly');
            }
            else {
                $('#myinput').removeAttr('readOnly');
                /* also tried  
                 * $('#myinput').attr('readOnly', false);
                 * $('#myinput').attr('readOnly', '');
                 */
            }
        });
        /*on text box click*/
        $('#myinput').click(function () {
            $('#mycheck').removeAttr('checked');
            $('#myinput').removeAttr('readOnly');
            /* also tried  
             * $('#myinput').attr('readOnly', false);
             * $('#myinput').attr('readOnly', '');
             */
        });
    </script>
</body>
</html>

Live copy

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about html