Populate second text box based on value extracted from first text box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sarega
    New Member
    • Oct 2008
    • 82

    Populate second text box based on value extracted from first text box

    Hi,
    I have to extract the value entered in the first text box and based on the value entered have to populate the second text box.
    Eg:if the value entered in first text box is like home/version then I have to extract the home and version entered in the text box and the second text box should contain home/version along with that it should contain additional say date information home/version/date.
    Can somebody help me do it?

    Thanks
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Post what code you have so far. Once you access an element using document.getEle mentById(), you can use the value property to get/set values - see link.

    PS. Please Use a Good Thread Title in future. Thanks!

    Comment

    • sarega
      New Member
      • Oct 2008
      • 82

      #3
      Urgent-Setting value of a textbox based on another in javascript

      Hi,
      I have to populate one text box based on the value entered in the previous text box
      eg:if the first text box contains abc/efg/hij then the next text box should contain abc/efg/xyz only if the first text box has abc/efg otherwise user has to enter the value into the next text box.

      Thanks

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Threads merged. Please do not double post your questions. Thanks.

        Moderator.

        Comment

        • sarega
          New Member
          • Oct 2008
          • 82

          #5
          Code:
          </head>
           <script type='text/javascript'>
           function test1()
           {
              var a = document.getElementById("first").value;
              var b = a.split('/');
              if( b[0] ==  "abc"){
              document.getElementById("second").setAttribute("value",a);
              }
           }
           </script>
           <body>
           <input type="text" id="first" onChange="test1();"/><br><br>
           <input type="text" id="second"/>
           </body>
           </html>
          this is the code that I am using can i use a function inside setAttribute?
          Last edited by Dormilich; Jan 22 '09, 10:33 AM. Reason: fixed [code] tags

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Yes, you can replace 'a' with a function call as long as it returns a string.

            PS. please use code tags when posting code. Thanks.

            Comment

            • sarega
              New Member
              • Oct 2008
              • 82

              #7
              Code:
              <script type='text/javascript'>
               function test1()
               {
                  var a = document.getElementById("first").value;
                  var b = a.split('/');
                  var d =b[0];
                  var c = b[1];
                  if( b[0] ==  "abc"){
                  document.getElementById("second").setAttribute("value",test2(d,c));
                  }
               }
              
               function test2(d,c)
               {
                      if ( c == "def" ) {
                      var f = "klm";
                      var x = d+'/'+c+'/'+f;
                      return x;
                      }
              }
              
               </script>
               <body>
               <input type="text" id="first" onChange="test1();"/><br><br>
               <input type="text" id="second"/>
              what is wrong with this code? am not getting the required result
              Last edited by acoder; Oct 16 '08, 07:12 AM. Reason: Added [code] tags

              Comment

              • zaphod42
                New Member
                • Oct 2008
                • 55

                #8
                Code:
                function test1(){ 
                var a = document.getElementById("first").value;
                var b = a.split('/');
                var d =b[0];
                var c = b[1];
                if((d == "abc")&&(c == "klm")){
                document.getElementById("second").value=test2(d,c)
                }
                }
                function test2(d,c){
                var f = "klm";
                var x = d+'/'+c+'/'+f;
                return x;
                }

                Comment

                • sarega
                  New Member
                  • Oct 2008
                  • 82

                  #9
                  can I include in the setAttribute("v alue",test2(c,d )) a function which is not within the javascript scope but a function which is within the scope of the html/php?
                  Code:
                   <script type='text/javascript'>
                   function test1()
                   {
                      var a = document.getElementById("first").value;
                      var b = a.split('/');
                      var d =b[0];
                      var c = b[1];
                      if( b[0] ==  "abc"){
                      document.getElementById("second").setAttribute("value",test2(d,c));
                      }
                   }
                  
                   function test2(d,c)
                   {
                          if ( c == "def" ) {
                          var f = "2009";
                          var x = c+'/'+'xyz'+'/'+f;
                          return x;
                          }
                  }
                  can I use functions which are not present in this particular file to be called in the javascript function test2()
                  Last edited by acoder; Oct 16 '08, 07:21 AM. Reason: Added [code] tags

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    As long as it's within the same page, it should be OK.

                    When you say you don't get the required result, please elaborate. What do you expect and what do you get instead?

                    PS. Please enclose your posted code in [code] tags (See How to Ask a Question). Thanks.

                    Comment

                    • sarega
                      New Member
                      • Oct 2008
                      • 82

                      #11
                      if the function is not present in the present page then how do I access that function in the javascript function??

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        Then include it using script tags, e.g.
                        Code:
                        <script type="text/javascript" src="file.js"></script>

                        Comment

                        • sarega
                          New Member
                          • Oct 2008
                          • 82

                          #13
                          Actually what I wanted is the values b[0] and b[1] has to be passed to a php function where the processing will be done and then the result of that php function has to be returned to the setAttribute.

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            Why didn't you say that earlier?! ;)

                            What you need is Ajax (if you want to avoid a page reload) unless you can convert the PHP function to JavaScript. If you're new to Ajax, check out the links in the Off-site Links sticky.

                            Comment

                            • sarega
                              New Member
                              • Oct 2008
                              • 82

                              #15
                              Code:
                              <?php
                                   $c=$_GET['c'];
                                   $d=$_GET['d'];
                                   if($c=="cat") {
                                      $e=$d.$c;
                                      }
                                   ?>
                              <script type='text/javascript'>
                              function test1()
                              {
                                   var a = document.getElementById("first").value;
                                   var b = a.split('/');
                                    var d =b[0];
                                    var c = b[1];
                                    if( b[0] ==  "dog"){
                                    location.href="c.php?d="+d+"&c="+c;
                                     var js= <?php echo $e; ?>
                                     document.getElementById("second").setAttribute("value",js);
                                     }
                              }
                              
                              </script>
                              <body>
                              <input type="text" id="first" size="40" onKeyUp="test1();"/><br><br>
                              <input type="text" id="second" size="40"/>
                              </body>
                              what is wrong with this code??
                              Last edited by acoder; Oct 16 '08, 11:14 AM. Reason: Added [code] tags

                              Comment

                              Working...