readonly attribute

Discussion in 'Javascript' started by Jimbo, Mar 14, 2006.

  1. Jimbo

    Jimbo Guest

    Hi,

    I am trying to set the readonly attribute of a text input tag dynamically
    from javascript. The input element already exists in the form and I want to
    make it readonly when a particular button is pressed. I have tried the
    following in javascript;

    function makeReadonly()
    {
    document.formName.inputTagName.setAttribute("readonly", "true");
    }

    However this doesn't have any effect. Should this work or should I be doing
    something else?

    Many thanks,

    Jimbo.
     
    Jimbo, Mar 14, 2006
    #1
    1. Advertising

  2. What browser are you using? A quick test of mine shows this works in
    Firefox but not IE. Perhaps IE does not support the standards for
    this?

    P.S. - You should be setting id in your tags not name -- and then use
    document.getElementById instead of document.formName.inputTagName.
     
    Justin McConnell, Mar 14, 2006
    #2
    1. Advertising

  3. Justin McConnell wrote:

    Are you responding to something?
    http://www.ah61.com/Quoting/googlequote.html

    > What browser are you using? A quick test of mine shows this works in
    > Firefox but not IE. Perhaps IE does not support the standards for
    > this?


    Internet Explorer 6 does not support setAttribute in HTML mode IIRC.

    > P.S. - You should be setting id in your tags not name -- and then use
    > document.getElementById instead of document.formName.inputTagName.


    Why? DOM 0 support is rather better than DOM 1 support isn't it?

    --
    David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
    Home is where the ~/.bashrc is
     
    David Dorward, Mar 14, 2006
    #3
  4. Jimbo

    RobG Guest

    Jimbo said on 15/03/2006 6:25 AM AEST:
    > Hi,
    >
    > I am trying to set the readonly attribute of a text input tag dynamically
    > from javascript. The input element already exists in the form and I want to
    > make it readonly when a particular button is pressed. I have tried the
    > following in javascript;
    >
    > function makeReadonly()
    > {
    > document.formName.inputTagName.setAttribute("readonly", "true");


    setAttribute has not been consistently implemented, use:


    document.formName.inputTagName.readonly = "true";


    In general it is safer to access DOM object properties directly rather
    than use setAttribute.


    > }
    >
    > However this doesn't have any effect. Should this work or should I be doing
    > something else?


    Yes, it *should* work, but that doesn't mean that it will. Because of
    the inconsistent implementation of host features, it is normal to use
    feature detection and guard against cases where what you are doing isn't
    supported or doesn't do what you expect, e.g.:

    var o;
    if ( (o = document) && (o = o.formName) && (o = o.inputTagName) ){
    o.readonly = true;
    } else {
    // didn't find element
    }

    And even though you may have set the readonly attribute of the DOM
    object to true, it may not actually make it readonly (though I expect it
    will in most browsers that get that far).


    --
    Rob
     
    RobG, Mar 14, 2006
    #4
  5. Yes, I'm responding to the root post in this thread. Is there a reason
    why I should quote the previous post when its right there at the top of
    the page?
     
    Justin McConnell, Mar 15, 2006
    #5
  6. RobG wrote :
    > Jimbo said on 15/03/2006 6:25 AM AEST:
    >> Hi,
    >>
    >> I am trying to set the readonly attribute of a text input tag
    >> dynamically from javascript. The input element already exists in the
    >> form and I want to make it readonly when a particular button is
    >> pressed. I have tried the following in javascript;
    >>
    >> function makeReadonly()
    >> {
    >> document.formName.inputTagName.setAttribute("readonly", "true");

    >
    > setAttribute has not been consistently implemented, use:
    >
    >
    > document.formName.inputTagName.readonly = "true";
    >


    It should be:

    document.formName.inputTagName.readOnly = true;

    http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-88461592

    >
    > In general it is safer to access DOM object properties directly rather
    > than use setAttribute.
    >
    >
    >> }
    >>
    >> However this doesn't have any effect. Should this work or should I be
    >> doing something else?

    >
    > Yes, it *should* work, but that doesn't mean that it will. Because of
    > the inconsistent implementation of host features, it is normal to use
    > feature detection and guard against cases where what you are doing isn't
    > supported or doesn't do what you expect, e.g.:
    >
    > var o;
    > if ( (o = document) && (o = o.formName) && (o = o.inputTagName) ){
    > o.readonly = true;




    It should be:

    o.readOnly = true;

    http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-88461592

    > } else {
    > // didn't find element
    > }
    >
    > And even though you may have set the readonly attribute of the DOM
    > object to true, it may not actually make it readonly (though I expect it
    > will in most browsers that get that far).
    >
    >



    Gérard
    --
    remove blah to email me
     
    Gérard Talbot, Mar 15, 2006
    #6
  7. Justin McConnell wrote :
    > Yes, I'm responding to the root post in this thread.


    Your posting should restore some context, should refer to something that
    you are replying to. Avoiding that defeats the purpose of archiving
    postings in newsgroups and eventual searching of archived posts.

    Consider a post which would only say:
    "Yes, that's correct." ... but we never get to see what was the question.

    Is there a reason
    > why I should quote the previous post when its right there at the top of
    > the page?


    Not everyone view/read posts in threads with google. Even google
    recommends to quote material to which you are replying to.

    "When you click 'Reply' under 'show options' to follow up an existing
    article, Google Groups includes the full article in quotes, with the
    cursor at the top of the article. Tempting though it is to just start
    typing your message, please STOP and do two things first. Look at the
    quoted text and remove parts that are irrelevant. Then, go to the BOTTOM
    of the article and start typing there. Doing this makes it much easier
    for your readers to get through your post. They'll have a reminder of
    the relevant text before your comment, but won't have to re-read the
    entire article.(...)"
    Group Help
    What's good 'netiquette' when posting to Usenet?
    http://groups.google.com/support/bin/answer.py?answer=12348&topic=250


    Gérard
    --
    remove blah to email me
     
    Gérard Talbot, Mar 15, 2006
    #7
  8. Jimbo

    Jimbo Guest

    Gérard,

    Brilliant. That works for me.

    Many thanks,

    Jimbo.


    "Gérard Talbot" <> wrote in message
    news:...
    > RobG wrote :
    >> Jimbo said on 15/03/2006 6:25 AM AEST:
    >>> Hi,
    >>>
    >>> I am trying to set the readonly attribute of a text input tag
    >>> dynamically from javascript. The input element already exists in the
    >>> form and I want to make it readonly when a particular button is pressed.
    >>> I have tried the following in javascript;
    >>>
    >>> function makeReadonly()
    >>> {
    >>> document.formName.inputTagName.setAttribute("readonly", "true");

    >>
    >> setAttribute has not been consistently implemented, use:
    >>
    >>
    >> document.formName.inputTagName.readonly = "true";
    >>

    >
    > It should be:
    >
    > document.formName.inputTagName.readOnly = true;
    >
    > http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-88461592
    >
    >>
    >> In general it is safer to access DOM object properties directly rather
    >> than use setAttribute.
    >>
    >>
    >>> }
    >>>
    >>> However this doesn't have any effect. Should this work or should I be
    >>> doing something else?

    >>
    >> Yes, it *should* work, but that doesn't mean that it will. Because of
    >> the inconsistent implementation of host features, it is normal to use
    >> feature detection and guard against cases where what you are doing isn't
    >> supported or doesn't do what you expect, e.g.:
    >>
    >> var o;
    >> if ( (o = document) && (o = o.formName) && (o = o.inputTagName) ){
    >> o.readonly = true;

    >
    >
    >
    > It should be:
    >
    > o.readOnly = true;
    >
    > http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-88461592
    >
    >> } else {
    >> // didn't find element
    >> }
    >>
    >> And even though you may have set the readonly attribute of the DOM object
    >> to true, it may not actually make it readonly (though I expect it will in
    >> most browsers that get that far).
    >>
    >>

    >
    >
    > Gérard
    > --
    > remove blah to email me
     
    Jimbo, Mar 15, 2006
    #8
    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. Matt
    Replies:
    1
    Views:
    4,185
  2. Matt
    Replies:
    2
    Views:
    19,095
  3. Hamed
    Replies:
    3
    Views:
    682
    Eliyahu Goldin
    Jul 9, 2006
  4. bruno modulix

    readonly class attribute ?

    bruno modulix, Mar 15, 2005, in forum: Python
    Replies:
    4
    Views:
    623
    Bruno Desthuilliers
    Mar 15, 2005
  5. Jonathan Hyatt
    Replies:
    1
    Views:
    2,019
    Jonathan Hyatt
    Jun 8, 2004
Loading...

Share This Page