How to make a html control be read only?

Discussion in 'HTML' started by Matt, Mar 6, 2004.

  1. Matt

    Matt Guest

    How to make a html control (text box, checkbox, etc...) be read only?
    For example, in the following text box code, value "Joe" is in text
    box, but user still able to edit it. I don't want user able to edit
    it.

    <input type="TEXT" name="username" value="Joe">

    Please advise!
    Matt, Mar 6, 2004
    #1
    1. Advertising

  2. Matt

    PW Guest

    "Matt" <> wrote in message
    news:...
    > How to make a html control (text box, checkbox, etc...) be read only?



    Try using "disabled" . (see example below)

    <input type=text name="Total" DISABLED value="" size="10" maxlength="10">

    You can also try "readonly".

    HTH,
    PW
    PW, Mar 6, 2004
    #2
    1. Advertising

  3. PW wrote:

    > "Matt" <> wrote in message
    > news:...
    >> How to make a html control (text box, checkbox, etc...) be read only?

    >
    > Try using "disabled" . (see example below)
    > <input type=text name="Total" DISABLED value="" size="10" maxlength="10">
    > You can also try "readonly".


    That is one possibility, but it begs the question of whether <input> is
    really an appropriate element to use in the first place.

    If Matt doesn't want the end user to be able to input a new value, why use
    <input> at all. Why not (for example) use:

    <span style="border: 1px solid black; padding:2px">Joe</span>

    --
    Toby A Inkster BSc (Hons) ARCS
    Contact Me - http://www.goddamn.co.uk/tobyink/?page=132
    Toby A Inkster, Mar 6, 2004
    #3
  4. Toby A Inkster <> wrote:

    >> Try using "disabled" . (see example below)
    >> <input type=text name="Total" DISABLED value="" size="10"
    >> maxlength="10"> You can also try "readonly".

    >
    > That is one possibility, but it begs the question of whether
    > <input> is really an appropriate element to use in the first place.


    Indeed. And I wouldn't advice anyone to give advice on such things
    unless the advisor _knows_ the difference beteen disabled and readonly
    and doesn't need to ask others to "try" it.

    > If Matt doesn't want the end user to be able to input a new value,
    > why use <input> at all. Why not (for example) use:
    >
    > <span style="border: 1px solid black; padding:2px">Joe</span>


    That would show (when CSS is enabled) the word "Joe" in a box, which
    would make it look a bit like an input field. I don't think that would
    be adequate in most cases, since it would suggest that the user _can_
    change it and it would mean that the string "Joe" is not passed to the
    form handler (which might not be needed actually, but I doubt that).

    ***

    The question was multiposted, so apparently the OP is not interested in
    getting quickly the best possible answer. For others, here's a copy of
    my response in alt.html.tags:

    (Matt) wrote:

    > How to make a html control (text box, checkbox, etc...) be read only?


    Using the attribute readonly (or if you play the XHTML game,
    readonly="readonly") in an <input> element. But by the specification,
    it applies to elements with type="text" and type="password" only
    (though browsers might be more liberal).

    > For example, in the following text box code, value "Joe" is in text
    > box, but user still able to edit it. I don't want user able to edit
    > it.
    >
    > <input type="TEXT" name="username" value="Joe">


    So why do you have the field there in the first place?

    It's safer to use

    <input type="hidden" name="username" value="Joe">
    Username: Joe

    partly because not quite all browsers support the readonly attribute,
    partly because this makes it much clearer that the user is not expected
    to change the username.

    --
    Yucca, http://www.cs.tut.fi/~jkorpela/
    Pages about Web authoring: http://www.cs.tut.fi/~jkorpela/www.html
    Jukka K. Korpela, Mar 6, 2004
    #4
  5. Matt wrote:
    > How to make a html control (text box, checkbox, etc...) be read only?
    > For example, in the following text box code, value "Joe" is in text
    > box, but user still able to edit it. I don't want user able to edit
    > it.


    Why do you want to do that? Input boxes are for _input_, not display.
    Why not put "Joe" on the page as normal text?
    Leif K-Brooks, Mar 6, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vnssoftware
    Replies:
    4
    Views:
    458
    Dmitry R
    Dec 31, 2003
  2. keithb
    Replies:
    2
    Views:
    7,991
    keithb
    Jun 7, 2006
  3. Phillip Vong

    VS2005 - Run Code only in Read Only mode.

    Phillip Vong, Apr 6, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    318
    Phillip Vong
    Apr 6, 2007
  4. Neal Becker

    buffer creates only read-only buffer?

    Neal Becker, Jan 8, 2009, in forum: Python
    Replies:
    0
    Views:
    403
    Neal Becker
    Jan 8, 2009
  5. datactrl
    Replies:
    4
    Views:
    158
    datactrl
    May 20, 2004
Loading...

Share This Page