-2

It is possible like php in javascript as below....

 <?php
     $var1 = "Hello";
     $var2 = "World";
  ?> 
    <input  type="text" value="<?php echo $var1; ?>" /> 
    <input  type="text" value="<?php echo $var2; ?>" />


    <script type="text/javascript">
       var str = "abc,def,ghi,jkl,mno,pqr";
        var x = str.split(","); 
     </script>

I have input type='text' in the body tag like

 <input id="txtOne" name="txtOne" type="text" value="I want to print here using javascript x[0]" /> 

 <input id="txtOne" name="txtOne" type="text" value="I want to print here using javascript x[1]" /> 

Thanks.

4
  • you can try like document.getElementById("txtOne").setAttribute("value",x[0]) Commented Nov 7, 2016 at 7:20
  • $('#txtOne').val(x[0]); Commented Nov 7, 2016 at 7:20
  • 2
    Possible duplicate of Passing javascript variable to html textbox Commented Nov 7, 2016 at 7:21
  • there's no way you'll be able you call a js variable inside value="x[0]" in html. you just let the javascript assign it's value Commented Nov 7, 2016 at 7:39

4 Answers 4

0

var str = "abc,def,ghi,jkl,mno,pqr";
var x = str.split(",");

var input = document.getElementById("txtOne");
input.value = input.value + x[0];
input {
    width: 400px;
  }
<input id="txtOne" name="txtOne" type="text" value="I wan't print here using javascript " />

Sign up to request clarification or add additional context in comments.

Comments

0

Without using any library, you need to work with the DOM.

This is probably what you want.

See the DEMO below

var str = "abc,def,ghi,jkl,mno,pqr";
var x = str.split(",");

var input = document.querySelectorAll('.txtOne');

for (var i = 0; i < input.length; i++){
  input[i].value = x[i];

}



 
<input class="txtOne" name="txtOne" type="text" value="" /> 
<br/>
 <input class="txtOne" name="txtOne" type="text" value="" /> 
<br/>
 <input class="txtOne" name="txtOne" type="text" value="" /> 

3 Comments

can you done with class var input = document.querySelectorAll('.txt');
Yes. You can do that as well. I have updated the answer.
Lot Of Thanks Abk
0
Create Cookies in page 1 (Create Cookies On this page )
    <script type="text/javascript">
         function createCookie(name,value,days) {
              if (days) {
                var date = new Date();
                date.setTime(date.getTime()+(days*24*60*60*1000));
                var expires = "; expires="+date.toGMTString();
          }
          else var expires = "";
               document.cookie = name+"="+value+expires+"; path=/";
          }
   var PartyDetail = $("#firstname").val() +"|"+$("#middlename").val()+"|"+$("#lastname").val();

  createCookie('cookiesPartyDetail',PartyDetail,2);
    </script>

  Now read cookies on second page

 <script type="text/javascript">
      function readCookie(name) {
          var nameEQ = name + "=";
          var ca = document.cookie.split(';');
          for(var i=0;i < ca.length;i++) {
               var c = ca[i];
               while (c.charAt(0)==' ') c = c.substring(1,c.length);
               if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
             }
            return null;
       }
     window.onload = function()
      {
          var str = readCookie('cookiesPartyDetail');
          //var str = "firstname,middlename,lastname";
          var x = str.split("|");
          for (i = 0; i < 3; i++) 
          { 
            $(".pending_order_"+[i]).val(x[i]);
          }

      } 

  function delete_cookie(name) {
    document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
     }
 </script>

 <input class="pending_order_0" id="firstname" name="firstname" type="text"  /> 
 <input class="pending_order_1" id="middlename" name="middlename" type="text" />
 <input class="pending_order_2" id="lastname" name="lastname" type="text" />

 if I want to use again in input type hidden field same like firstname, middlename, lastname my code is here..

 <input class="pending_order_0" id="firstname" name="firstname" type="hidden"  /> 
 <input class="pending_order_1" id="middlename" name="middlename" type="hidden" />
 <input class="pending_order_2" id="lastname" name="lastname" type="hidden" />

Comments

-1

Using Javascript or one of it's libraries you can do that.

Using Jquery:

$("#txtOne").val(x[0]);

2 Comments

I know this but i wan't to print in value="<script>document.write(x[0])</script>" it is possible..
Note: if jQuery is not tagged, answer based on it will attract downvotes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.