JavaScript coding to swap the content of two text box elements

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sreekandan
    New Member
    • Nov 2006
    • 98

    JavaScript coding to swap the content of two text box elements

    hi everyone....
    i want the coding help in Javascript to "swap the content of two Textbox elements" ..Kindly reply me.....
  • Rahaman sharif
    New Member
    • Sep 2007
    • 6

    #2
    Hi,

    Please explain clearly what u want exactly. swap the text in textbox means?

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by sreekandan
      hi everyone....
      i want the coding help in Javascript to "swap the content of two Textbox elements" ..Kindly reply me.....
      [code=HTML]
      <input type=text id=text_id1 value='' />
      <input type=text id=text_id2 value='' />
      [/code]

      [code=javascript]
      function swap_content(){
      var tmp = document.getEle mentById('text_ id1').value;
      document.getEle mentById('text_ id1').value = document.getEle mentById('text_ id2').value;
      document.getEle mentById('text_ id2').value = tmp;
      }
      [/code]

      Debasis Jana

      Comment

      • dkyadav80
        New Member
        • Jul 2008
        • 21

        #4
        same

        [code=HTML]
        <input "type="text " name="text_id1" id="text_id1" value='' access here value from the PHP or other,, eg. echo"$_POST['text_id1']"; "/>
        <input type="text" name="text_id1" id="text_id2" value='' access here value from the PHP or other eg. echo"$_POST['text_id2']"; ""//>
        <input type="submit" value="swap" onClick="swap_c ontent("text_id 1","text_id2"); ">
        [/code]

        [code=javascript]
        function swap_content(id 1,id2){
        var tmp = document.getEle mentById('text_ id1').value;
        document.getEle mentById('text_ id1').value = document.getEle mentById('text_ id2').value;
        document.getEle mentById('text_ id2').value = tmp;
        }
        [/code]

        I think this will be work good .


        dkyadav80
        Last edited by acoder; Jul 15 '08, 06:19 PM. Reason: removed quote

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Originally posted by dkyadav80
          I think this will be work good .
          There are one or two problems: you've got double quotes within double quotes, and you're passing the ids but not making use of them.

          Comment

          • sreekandan
            New Member
            • Nov 2006
            • 98

            #6
            i want to swap the content of two text box by using javascript...

            i want to swap the content of two text box by using javascript...I have written code as follows,but its not working....kind ly reply me any one as soon as possible....

            Code:
            <html>
            <input "type="text" name="text_id1" id="text_id1" value='a' >
            <input type="text" name="text_id1" id="text_id2" value='b' >
            <input type="submit" value="swap" onClick=swap_content(text_id1,text_id2)>
            <script type="javascript/text">
            function swap_content(id1,id2)
            {
             var tmp = document.getElementById(text_id1).value;
             document.getElementById(text_id1).value = document.getElementById(text_id2).value;
            document.getElementById(text_id2).value = tmp;
            }
            Last edited by acoder; Jul 17 '08, 10:54 AM. Reason: Added [code] tags

            Comment

            • sreekandan
              New Member
              • Nov 2006
              • 98

              #7
              i want to swap the content of two text box by using javascript...

              i want to swap the content of two text box by using javascript...I have written code as follows,but its not working....kind ly reply me any one as soon as possible....

              Code:
              <html>
              <input "type="text" name="text_id1" id="text_id1" value='a' >
              <input type="text" name="text_id1" id="text_id2" value='b' >
              <input type="submit" value="swap" onClick=swap_content(text_id1,text_id2)>
              <script type="javascript/text">
              function swap_content(id1,id2)
              {
              var tmp = document.getElementById(text_id1).value;
              document.getElementById(text_id1).value = document.getElementById(text_id2).value;
              document.getElementById(text_id2).value = tmp;
              }
              Last edited by acoder; Jul 17 '08, 10:55 AM. Reason: Added [code] tags

              Comment

              • hsriat
                Recognized Expert Top Contributor
                • Jan 2008
                • 1653

                #8
                [html]<input type="text" name="text_id1" id="text_id1" value="a" >
                <input type="text" name="text_id2" id="text_id2" value="b" >
                <input type="submit" value="swap" onclick="swap_content(' text_id1','text _id2')">
                <script type="text/javascript">
                function swap_content(id 1,id2)
                {
                var tmp = document.getEle mentById(id1).value;
                document.getEle mentById(id1).value = document.getEle mentById(id2).value;
                document.getEle mentById(id2).value = tmp;
                }
                </script>[/html]

                I've underlined the part of the code where you had made some mistakes.

                Comment

                • sreekandan
                  New Member
                  • Nov 2006
                  • 98

                  #9
                  Thank U...I got the output....

                  Originally posted by hsriat
                  [html]<input type="text" name="text_id1" id="text_id1" value="a" >
                  <input type="text" name="text_id2" id="text_id2" value="b" >
                  <input type="submit" value="swap" onclick="swap_content(' text_id1','text _id2')">
                  <script type="text/javascript">
                  function swap_content(id 1,id2)
                  {
                  var tmp = document.getEle mentById(id1).value;
                  document.getEle mentById(id1).value = document.getEle mentById(id2).value;
                  document.getEle mentById(id2).value = tmp;
                  }
                  </script>[/html]

                  I've underlined the part of the code where you had made some mistake.

                  Comment

                  • debasisdas
                    Recognized Expert Expert
                    • Dec 2006
                    • 8119

                    #10
                    question moved to javascript forum.

                    Comment

                    • hsriat
                      Recognized Expert Top Contributor
                      • Jan 2008
                      • 1653

                      #11
                      Originally posted by debasisdas
                      question moved to javascript forum.
                      Question already there in JS forum. Both threads just need to be merged.

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        sreekandan, as a full member now, you should know that we expect your code to be posted in [CODE] tags (See How to Ask a Question).

                        This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

                        Please use the tags in future.

                        Also, do not double/triple post your questions. All duplicate threads have been merged.

                        MODERATOR.

                        Comment

                        Working...