inserting content into textarea

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • emekadavid
    New Member
    • Mar 2007
    • 46

    inserting content into textarea

    how can i use a javascript function to dynamically insert content into a textarea tag ?
    is this possible? i've read tutorials on the doms and can't find any possibility.
    thanks
  • iam_clint
    Recognized Expert Top Contributor
    • Jul 2006
    • 1207

    #2
    Example provided.
    [CODE=javascript]
    <textarea id="wow" name="wow"></textarea>
    <script>
    document.getEle mentById("wow") .innerHTML = "This is the coolest site in the world!";
    </script>
    [/CODE]

    Comment

    • invincible
      New Member
      • Jul 2007
      • 2

      #3
      Originally posted by iam_clint
      Example provided.
      [CODE=javascript]
      <textarea id="wow" name="wow"></textarea>
      <script>
      document.getEle mentById("wow") .innerHTML = "This is the coolest site in the world!";
      </script>
      [/CODE]

      Or you may simplay write the below code

      <script>
      document.getEle mentById("wow") .value = "This is the coolest site in the world!";
      </script>

      Comment

      • ianhobson
        New Member
        • Jul 2007
        • 6

        #4
        Originally posted by invincible
        Or you may simply write the below code

        <script>
        document.getEle mentById("wow") .value = "This is the coolest site in the world!";
        </script>
        This only works in IE6 and before.

        In Firefox et al, you need
        Code:
        <script>
        document.getElementById("wow").defaultValue = "This is the coolest site in the world!";
        </script>
        InnerHTML works on all browsers I have been able to test it on.

        Does anyone know how to do this for IE7? None of the above methods work on my customer's site (and I can't run IE7 as I use win2K).

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5388

          #5
          hi ...

          i don't use IE ... i'm on a mac ;) ... but perhaps the following will work:

          [CODE=javascript]
          var text_node = document.create TextNode('text to append');
          var text_area = document.getEle mentById("wow") ;

          text_area.appen dChild(text_nod e);
          [/CODE]

          kind regards

          Comment

          • ianhobson
            New Member
            • Jul 2007
            • 6

            #6
            Originally posted by gits
            hi ...

            i don't use IE ... i'm on a mac ;) ... but perhaps the following will work:

            [CODE=javascript]
            var text_node = document.create TextNode('text to append');
            var text_area = document.getEle mentById("wow") ;

            text_area.appen dChild(text_nod e);
            [/CODE]

            kind regards
            Good idea, but sadly it doesn't work either.

            My code is currently
            Code:
            <script type="text/javascript">
            function changeChassis() {
            var t = document.forms[0].chassistype.selectedIndex;
            var v = document.forms[0].chassistype.options[t].value;
            var res = 'junk';
            if(v == 'DAF LF55 220 4x2 Day cab 3750mm') 
               res = 'DAF LF55 220 4x2 Day Cab 3750mm wheelbase 18 Tonnes G.V.W';
            
            -- loads more if statements generated from database --
            
            if(v == 'Volvo FM400 8x4 Globetrotter Cab 5100mm') 
               res = 'Volvo FM400 8x4 Globetrotter Cab 5100mm wheelbase 32 Tonnes G.V.W';
            if(v == 'Volvo FM440 8x4 Sleeper Cab 5600mm') 
               res = 'Volvo FM440 8x4 Sleeper Cab 5600mm wheelbase 32 Tonnes G.V.W';
            var fld = document.forms[0].chassisdescription;
            var node = document.createTextNode(res);
            fld.replaceChild(node,fld.childNodes[0]);
            }
            </script>
            chassisdescript ion is the textarea.
            chassistype is the selection.
            This is the onchange routine for chassistype.

            Idea is to set the default to a fuller description of the chassis when one is selected - and permit later edits.

            The code works fine in Safari, Firefox, IE6 on Win2K, but fails on the customer's site (Win Server 2003/ EI7).

            I'm going bald trying to find out why.

            Ian

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5388

              #7
              hmmmm ... can you try to figure out what 'fld.childNodes[0]' is?

              [CODE=javascript]
              fld.replaceChil d(node, fld.childNodes[0]);
              [/CODE]
              try an alert of nodeName or simply try to use appendChild ... my be replaceChild needs another reference for the node to be replaced? in case appendChild works we remove everything in the textarea and append the new one ;)

              kind regards

              Comment

              • ianhobson
                New Member
                • Jul 2007
                • 6

                #8
                Solved!

                I have just got another user at the client site to test the code - it works for him!

                So the problem is something to do with the set up on a single machine - and not IE7 at all.

                The code in my previous post works in IE7.

                Regards

                Ian

                Comment

                • gits
                  Recognized Expert Moderator Expert
                  • May 2007
                  • 5388

                  #9
                  glad to hear ... come back anytime when you have more questions ...

                  kind regards

                  Comment

                  • iam_clint
                    Recognized Expert Top Contributor
                    • Jul 2006
                    • 1207

                    #10
                    Yes I was going to post that .innerHTML works on firefox, ie6, opera, ie7, and probably more but i haven't tested

                    Comment

                    Working...