Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Apr 2007
    Posts
    179
    Thanks
    17
    Thanked 0 Times in 0 Posts

    dynamically set readonly

    There are two ways for me to make a input field readonly:

    1)hardcoding:

    In a JSP, I use <input type="text" id="test" readonly="readonly" value="1234abc" />

    The field will become readonly--I can not edit the text in the field. This is what I wanted.

    2) Dynamical:

    But sometimes I need to achieve this at the runtime/dynamically:

    <input type="text" value="1234abc" />

    Now in the javascript:

    document.getElementById("test").readonly = true;

    The field will display as readonly. However, I can actually highlight the text, delete and edit it.

    Why so and how can I solve this problem?

    Thanks

    Scott

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,146
    Thanks
    39
    Thanked 505 Times in 499 Posts

    Lightbulb Close ...

    Try this:
    Code:
    <html>
    <head>
    <title>Dynamic ReadOnly</title>
    </head>
    <body>
    <!-- input type="text" id="test" readonly="readonly" value="1234abc" / -->
    <input type="text" id="test" disabled value="1234abc" />
    <br>
    <button onclick="document.getElementById('test2').disabled=false">Reset ReadOnly</button>
    <input type="text" id="test2" value="1234abc" />
    <button onclick="document.getElementById('test2').disabled=true">Set ReadOnly</button>
    </body>
    </html>
    Last edited by jmrker; 01-22-2009 at 03:53 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •