change textbox readonly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nirmalsingh
    New Member
    • Sep 2006
    • 218

    change textbox readonly

    hai all,
    in html i have created a text box and make it readonly such as

    <input type="text" id="age" readonly/>


    now i want to make it enable(through javascript) to type in text box . what should i do for this?
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    [code=javascript]
    obj_ref.readOnl y = false;
    //or obj_ref.setAttr ibute("readonly ") = false;
    [/code]

    kind regards.
    dmjpro.

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5388

      #3
      Originally posted by dmjpro
      [code=javascript]
      obj_ref.readOnl y = false;
      //or obj_ref.setAttr ibute("readonly ") = false;
      [/code]

      kind regards.
      dmjpro.
      hi ... please excuse me and may be i'm wrong ... but i think that will not work, as far as i know ... most browsers look for existence of the readonly attribute ... and therefore it works without having a value assigned:

      [HTML]<input type="text" id="age" readonly/>[/HTML]

      so the right way is to remove that attribute from that element, you may use a function like the shown one below:

      [CODE=javascript]
      /**
      * @param obj - reference to element where attr should be changed (use:
      * document.getEle mentById('id') or whatever you want for this)
      * @param - state true or false
      */
      function set_readonly_st ate(obj, state) {
      if (state) {
      obj.setAttribut e('readonly', state);
      } else {
      obj.removeAttri bute('readonly' );
      }
      }[/CODE]

      kind regards ...

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by gits
        hi ... please excuse me and may be i'm wrong ... but i think that will not work, as far as i know ...
        Yes, it should work, see link. I tried some sample code in Firefox and it works fine.

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5388

          #5
          Originally posted by acoder
          Yes, it should work, see link. I tried some sample code in Firefox and it works fine.
          hmmm ... thats right ... i'm sorry! but using this way is a little bit different from my point of view ... until there is a difference between setting an attribute or using a property of an element. the code i provided, simply uses the readonly attribute of the input-element. I didn't know about the 'readOnly' property of the element and of course this works too ... but i think it is good (coding-)practice to use only one way of setting ui-element-states that means use properties OR use attributes (in case they do the 'same') ... i hope you understand what i'm trying to say? the input-element in our example is set to readonly-state through an attribute ... setAttribute it to false will not work ... you have to remove it to enable the widget.

          if you prefer to set properties ... than it works fine too ... of course, but it mixes the possibilities and in larger apps this is hard to debug, or to find ... it's only an idea of me and my personal point of view ... you may agree or disagree with it ... ;)

          kind regards ...

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by gits
            ... it's only an idea of me and my personal point of view ... you may agree or disagree with it ... ;)
            I don't disagree with what you're saying. I was merely pointing out that the readOnly property does exist.

            Comment

            Working...