Javascript to hide a text box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • avr1983
    New Member
    • Mar 2007
    • 10

    Javascript to hide a text box

    hi,

    would you please help me in writing a javascript code to implement a text box that is hidden beside a drop down box.the text box should be enabled only when we select an "others"opt ion from a drop down box.please help me,its urgent.

    Thank you in Advance........
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Welcome to TSDN.

    Should it be hidden or disabled?

    You'll need to use the onchange event on the select:
    [HTML]<select ... onchange="...">[/HTML] Call a function that will enable/show the text box. Assuming txtobj is the text box object and selObj is the select object:
    Code:
    if (selObj.value=="Other") txtobj.disabled=false;
    else txtobj.disabled=true;

    Comment

    • avr1983
      New Member
      • Mar 2007
      • 10

      #3
      Thank-U for your reply.

      The text box should be hidden when we select specifications in given combo box and when we select "others" in that specifications text box should be visible to write into it

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Post your code/HTML that you've got so far.

        Try and follow my first post replacing
        Code:
        disabled=false
        with
        Code:
        style.visibility='visible'
        and
        Code:
        disabled=true
        with
        Code:
        style.visibility='hidden'
        to make visible/hide instead of enable/disable.

        Comment

        • avr1983
          New Member
          • Mar 2007
          • 10

          #5
          need a bit clear code....
          thank u in advance.....

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by avr1983
            need a bit clear code....
            thank u in advance.....
            No it doesn't work like that.

            What code have you got so far? What errors have you got?

            Comment

            • avr1983
              New Member
              • Mar 2007
              • 10

              #7
              exactly what i need is,we have a combo box with some value in it.when i select the value "others" in d combo box i want the text box to be appeared.and for remaining values it should be hidden.
              thank u in advance

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Agan, you'll need to use the onchange event on the select:
                [HTML]<select ... onchange="myfun (this)">[/HTML] "this" refers to this element meaning the select element.
                In your function, assuming txtobj is the text box object (you know how to access that):
                [CODE=javascript]function myfun(selObj) {
                if (selObj.value== "Other") txtobj.style.vi sibility='visib le';
                else txtobj.style.vi sibility='hidde n';
                }[/CODE]

                Comment

                • simplesoftsolutions
                  New Member
                  • May 2007
                  • 5

                  #9
                  An alternative to visibility (if I'm not mistaken visibility doesn't work in all browsers, am I correct?) you can use OBJECTNAME.styl e.display = "none;". As someone else mentioned, what code do you have already?

                  Anthony
                  simplesoftsolut ions.com

                  Comment

                  • nadinechemali
                    New Member
                    • May 2007
                    • 1

                    #10
                    Hi guys well i have the same prob i have a select list with names of companies
                    and i have at the end a field called others
                    i want that when i choose others that an empty appear but ur code didnt work:S
                    plz help! they told me that it can work with ajax!
                    Last edited by nadinechemali; May 18 '07, 06:23 PM. Reason: ...

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      Originally posted by nadinechemali
                      Hi guys well i have the same prob i have a select list with names of companies
                      and i have at the end a field called others
                      i want that when i choose others that an empty appear but ur code didnt work:S
                      plz help! they told me that it can work with ajax!
                      Welcome to TSDN.

                      You don't need ajax for this - it just requires some simple DHTML. Post your code so we can see your problem.

                      Comment

                      Working...