Html and Javascript: How to return user inputHow do JavaScript closures work?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How do I redirect to another webpage?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?Why does HTML think “chucknorris” is a color?How to use foreach with array in JavaScript?

Why does this relative pronoun not take the case of the noun it is referring to?

Could a US political party gain complete control over the government by removing checks & balances?

What does "enim et" mean?

Does the radius of the Spirit Guardians spell depend on the size of the caster?

Should I join an office cleaning event for free?

How would photo IDs work for shapeshifters?

cryptic clue: mammal sounds like relative consumer (8)

When blogging recipes, how can I support both readers who want the narrative/journey and ones who want the printer-friendly recipe?

How do we improve the relationship with a client software team that performs poorly and is becoming less collaborative?

How do I create uniquely male characters?

Need help identifying/translating a plaque in Tangier, Morocco

Why Is Death Allowed In the Matrix?

Showing the closure of a compact subset need not be compact

Prevent a directory in /tmp from being deleted

least quadratic residue under GRH: an EXPLICIT bound

How can the DM most effectively choose 1 out of an odd number of players to be targeted by an attack or effect?

How to determine if window is maximised or minimised from bash script

Why don't electron-positron collisions release infinite energy?

Schwarzschild Radius of the Universe

What Brexit solution does the DUP want?

Is there a way I can return more than one integer from a method?

Is there a familial term for apples and pears?

How to delete an array properly in Java

Shell script can be run only with sh command



Html and Javascript: How to return user input


How do JavaScript closures work?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How do I redirect to another webpage?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?Why does HTML think “chucknorris” is a color?How to use foreach with array in JavaScript?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I have 3 functions that, when the user enters their first and last name in the message box, it returns (a)Their name merged in array form and (b)Their names merged as well but in an array of objects.
My problem is that when I test it in Google Chrome, nothing shows up when I press the Save button, but in JSbin it just returns:



[object Object]


This is the functions code (useMerge.js):



var first_name = [];
var last_name = [];

var fInput = document.getElementById("fname");
var lInput = document.getElementById("lname");
var messageBox = document.getElementById("display");

function insert ( )
first_name.push( fInput.value );
last_name.push( lInput.value );

Show();


function Show ()
//clear messagebox
fInput.value = "";
lInput.value = "";

//show
messageBox.innerHTML = "";

messageBox.innerHTML = mergeHandler(first_name,last_name,merge2Single);
messageBox.innerHTML = mergeHandler(first_name,last_name,merge2Object);


//merges
function merge2Single(first, last)
var arr=[];
arr.push(first);
arr.push(last);
return arr.join(" ");


function mergeHandler(arr1, arr2, func)
var i=0;
var size = arr1.length;
var result=[];
for(i=0; i<size; i++)
result.push(func(arr1[i], arr2[i]));

return result;


function merge2Object(first_name, last_name)
var arr=[];
arr.push(FirstName: first_name,
LastName: last_name);

return arr;



Html code:



<!DOCTYPE html>
<html>
<head>
<script src="useMerge.js" type="text/javascript"></script>
<meta charset=utf-8 />
<title>UI</title>

<style>
article, aside, figure, footer, header, hgroup,
menu, nav, section
display: block;

</style>
</head>
<body>
<form>
<h1>Please enter data</h1>
<input id="fname" type="text" placeholder="First Name" />
<input id="lname" type="text" placeholder="Last Name" />
<input type="button" value="Save" onclick="insert()" />
</form>
<div id="display"></div>
</body>
</html>









share|improve this question



















  • 1





    add the defer attribute to your script tag for useMerge.js

    – Randy Casburn
    Mar 9 at 4:04

















0















I have 3 functions that, when the user enters their first and last name in the message box, it returns (a)Their name merged in array form and (b)Their names merged as well but in an array of objects.
My problem is that when I test it in Google Chrome, nothing shows up when I press the Save button, but in JSbin it just returns:



[object Object]


This is the functions code (useMerge.js):



var first_name = [];
var last_name = [];

var fInput = document.getElementById("fname");
var lInput = document.getElementById("lname");
var messageBox = document.getElementById("display");

function insert ( )
first_name.push( fInput.value );
last_name.push( lInput.value );

Show();


function Show ()
//clear messagebox
fInput.value = "";
lInput.value = "";

//show
messageBox.innerHTML = "";

messageBox.innerHTML = mergeHandler(first_name,last_name,merge2Single);
messageBox.innerHTML = mergeHandler(first_name,last_name,merge2Object);


//merges
function merge2Single(first, last)
var arr=[];
arr.push(first);
arr.push(last);
return arr.join(" ");


function mergeHandler(arr1, arr2, func)
var i=0;
var size = arr1.length;
var result=[];
for(i=0; i<size; i++)
result.push(func(arr1[i], arr2[i]));

return result;


function merge2Object(first_name, last_name)
var arr=[];
arr.push(FirstName: first_name,
LastName: last_name);

return arr;



Html code:



<!DOCTYPE html>
<html>
<head>
<script src="useMerge.js" type="text/javascript"></script>
<meta charset=utf-8 />
<title>UI</title>

<style>
article, aside, figure, footer, header, hgroup,
menu, nav, section
display: block;

</style>
</head>
<body>
<form>
<h1>Please enter data</h1>
<input id="fname" type="text" placeholder="First Name" />
<input id="lname" type="text" placeholder="Last Name" />
<input type="button" value="Save" onclick="insert()" />
</form>
<div id="display"></div>
</body>
</html>









share|improve this question



















  • 1





    add the defer attribute to your script tag for useMerge.js

    – Randy Casburn
    Mar 9 at 4:04













0












0








0








I have 3 functions that, when the user enters their first and last name in the message box, it returns (a)Their name merged in array form and (b)Their names merged as well but in an array of objects.
My problem is that when I test it in Google Chrome, nothing shows up when I press the Save button, but in JSbin it just returns:



[object Object]


This is the functions code (useMerge.js):



var first_name = [];
var last_name = [];

var fInput = document.getElementById("fname");
var lInput = document.getElementById("lname");
var messageBox = document.getElementById("display");

function insert ( )
first_name.push( fInput.value );
last_name.push( lInput.value );

Show();


function Show ()
//clear messagebox
fInput.value = "";
lInput.value = "";

//show
messageBox.innerHTML = "";

messageBox.innerHTML = mergeHandler(first_name,last_name,merge2Single);
messageBox.innerHTML = mergeHandler(first_name,last_name,merge2Object);


//merges
function merge2Single(first, last)
var arr=[];
arr.push(first);
arr.push(last);
return arr.join(" ");


function mergeHandler(arr1, arr2, func)
var i=0;
var size = arr1.length;
var result=[];
for(i=0; i<size; i++)
result.push(func(arr1[i], arr2[i]));

return result;


function merge2Object(first_name, last_name)
var arr=[];
arr.push(FirstName: first_name,
LastName: last_name);

return arr;



Html code:



<!DOCTYPE html>
<html>
<head>
<script src="useMerge.js" type="text/javascript"></script>
<meta charset=utf-8 />
<title>UI</title>

<style>
article, aside, figure, footer, header, hgroup,
menu, nav, section
display: block;

</style>
</head>
<body>
<form>
<h1>Please enter data</h1>
<input id="fname" type="text" placeholder="First Name" />
<input id="lname" type="text" placeholder="Last Name" />
<input type="button" value="Save" onclick="insert()" />
</form>
<div id="display"></div>
</body>
</html>









share|improve this question
















I have 3 functions that, when the user enters their first and last name in the message box, it returns (a)Their name merged in array form and (b)Their names merged as well but in an array of objects.
My problem is that when I test it in Google Chrome, nothing shows up when I press the Save button, but in JSbin it just returns:



[object Object]


This is the functions code (useMerge.js):



var first_name = [];
var last_name = [];

var fInput = document.getElementById("fname");
var lInput = document.getElementById("lname");
var messageBox = document.getElementById("display");

function insert ( )
first_name.push( fInput.value );
last_name.push( lInput.value );

Show();


function Show ()
//clear messagebox
fInput.value = "";
lInput.value = "";

//show
messageBox.innerHTML = "";

messageBox.innerHTML = mergeHandler(first_name,last_name,merge2Single);
messageBox.innerHTML = mergeHandler(first_name,last_name,merge2Object);


//merges
function merge2Single(first, last)
var arr=[];
arr.push(first);
arr.push(last);
return arr.join(" ");


function mergeHandler(arr1, arr2, func)
var i=0;
var size = arr1.length;
var result=[];
for(i=0; i<size; i++)
result.push(func(arr1[i], arr2[i]));

return result;


function merge2Object(first_name, last_name)
var arr=[];
arr.push(FirstName: first_name,
LastName: last_name);

return arr;



Html code:



<!DOCTYPE html>
<html>
<head>
<script src="useMerge.js" type="text/javascript"></script>
<meta charset=utf-8 />
<title>UI</title>

<style>
article, aside, figure, footer, header, hgroup,
menu, nav, section
display: block;

</style>
</head>
<body>
<form>
<h1>Please enter data</h1>
<input id="fname" type="text" placeholder="First Name" />
<input id="lname" type="text" placeholder="Last Name" />
<input type="button" value="Save" onclick="insert()" />
</form>
<div id="display"></div>
</body>
</html>






javascript html arrays json






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 9 at 4:05







B.Uriah

















asked Mar 9 at 4:00









B.UriahB.Uriah

174




174







  • 1





    add the defer attribute to your script tag for useMerge.js

    – Randy Casburn
    Mar 9 at 4:04












  • 1





    add the defer attribute to your script tag for useMerge.js

    – Randy Casburn
    Mar 9 at 4:04







1




1





add the defer attribute to your script tag for useMerge.js

– Randy Casburn
Mar 9 at 4:04





add the defer attribute to your script tag for useMerge.js

– Randy Casburn
Mar 9 at 4:04












4 Answers
4






active

oldest

votes


















0














You are putting an object in display. Use JSON.stringify(your_obj), this will convert your object into string.






share|improve this answer






























    1














    You should read the textbox after the user submit the button inside the function as shown below :



    var first_name = [];
    var last_name = [];

    var fInput;
    var lInput;
    var messageBox;

    function insert ( )
    fInput = document.getElementById("fname");
    lInput = document.getElementById("lname");
    messageBox = document.getElementById("display");
    first_name.push( fInput.value );
    last_name.push( lInput.value );

    Show();


    function Show ()
    //clear messagebox
    fInput.value = "";
    lInput.value = "";

    //show
    messageBox.innerHTML = "";
    messageBox.innerHTML = mergeHandler(first_name,last_name,merge2Single);
    messageBox.innerHTML = mergeHandler(first_name,last_name,merge2Object);


    //merges
    function merge2Single(first, last)
    var arr=[];
    arr.push(first);
    arr.push(last);
    return arr.join(" ");


    function mergeHandler(arr1, arr2, func)
    var i=0;
    var size = arr1.length;
    var result=[];
    for(i=0; i<size; i++)
    result.push(func(arr1[i], arr2[i]));

    return result;


    function merge2Object(first_name, last_name)
    var arr=[];
    arr.push(FirstName: first_name,
    LastName: last_name);

    return arr;



    After this fix, you get the following result:



    [object Object]





    share|improve this answer






























      0














      Primarily I can see two issue in your code




      1. You are loading the script before dom is ready so



        var fInput = document.getElementById("fname");
        var lInput = document.getElementById("lname");
        var messageBox = document.getElementById("display");



        js will not able to find these elements from the dom



        To solve this issue load js near closing end of body



        <body>
        // rest of html
        <script src="useMerge.js" type="text/javascript"></script>
        </body>



      2. If you want to join the first name and last number array , just concat those two arrays and then use join





      var first_name = [];
      var last_name = [];

      var messageBox = document.getElementById("display");
      var messageBox2 = document.getElementById("display2");

      function insert()
      var fInput = document.getElementById("fname");
      var lInput = document.getElementById("lname");
      first_name.push(fInput.value);
      last_name.push(lInput.value);

      Show();


      function Show()
      //clear messagebox
      fInput.value = "";
      lInput.value = "";

      //show
      messageBox.innerHTML = "";
      messageBox2.innerHTML = "";

      messageBox.innerHTML = mergeHandler(first_name, last_name, merge2Single);
      messageBox2.innerHTML = JSON.stringify(mergeHandler(first_name, last_name, merge2Object));


      //merges
      function merge2Single(first, last)
      return first_name.concat(last_name).join(' ');


      function mergeHandler(arr1, arr2, func)
      return func(arr1, arr2);


      function merge2Object(first_name, last_name)
      var arr = [];
      arr.push(
      FirstName: first_name,
      LastName: last_name
      );

      return arr;

      article,
      aside,
      figure,
      footer,
      header,
      hgroup,
      menu,
      nav,
      section
      display: block;

      <form>
      <h1>Please enter data</h1>
      <input id="fname" type="text" placeholder="First Name" />
      <input id="lname" type="text" placeholder="Last Name" />
      <input type="button" value="Save" onclick="insert()" />
      </form>
      <div id="display"></div>
      <div id="display2"></div>








      share|improve this answer






























        0














        Try this in this function. Just map the object and joint the strings. By the way, you are overriding the results of your functions by trying to display the text in the same div



        Finally use the attribute defer in your <script> tag. Right now your javascript in the browser is running before your html is loaded. Just do this:



        <script src="script.js" type="text/javascript" defer></script>



        function merge2Object(first_name, last_name)
        var arr=[];
        arr.push(FirstName: first_name,
        LastName: last_name);

        console.log(arr);
        return arr.map(e => `$e.FirstName $e.LastName`).join(",");






        share|improve this answer























          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073851%2fhtml-and-javascript-how-to-return-user-input%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You are putting an object in display. Use JSON.stringify(your_obj), this will convert your object into string.






          share|improve this answer



























            0














            You are putting an object in display. Use JSON.stringify(your_obj), this will convert your object into string.






            share|improve this answer

























              0












              0








              0







              You are putting an object in display. Use JSON.stringify(your_obj), this will convert your object into string.






              share|improve this answer













              You are putting an object in display. Use JSON.stringify(your_obj), this will convert your object into string.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Mar 9 at 4:06









              Arnold PargeArnold Parge

              1,81611319




              1,81611319























                  1














                  You should read the textbox after the user submit the button inside the function as shown below :



                  var first_name = [];
                  var last_name = [];

                  var fInput;
                  var lInput;
                  var messageBox;

                  function insert ( )
                  fInput = document.getElementById("fname");
                  lInput = document.getElementById("lname");
                  messageBox = document.getElementById("display");
                  first_name.push( fInput.value );
                  last_name.push( lInput.value );

                  Show();


                  function Show ()
                  //clear messagebox
                  fInput.value = "";
                  lInput.value = "";

                  //show
                  messageBox.innerHTML = "";
                  messageBox.innerHTML = mergeHandler(first_name,last_name,merge2Single);
                  messageBox.innerHTML = mergeHandler(first_name,last_name,merge2Object);


                  //merges
                  function merge2Single(first, last)
                  var arr=[];
                  arr.push(first);
                  arr.push(last);
                  return arr.join(" ");


                  function mergeHandler(arr1, arr2, func)
                  var i=0;
                  var size = arr1.length;
                  var result=[];
                  for(i=0; i<size; i++)
                  result.push(func(arr1[i], arr2[i]));

                  return result;


                  function merge2Object(first_name, last_name)
                  var arr=[];
                  arr.push(FirstName: first_name,
                  LastName: last_name);

                  return arr;



                  After this fix, you get the following result:



                  [object Object]





                  share|improve this answer



























                    1














                    You should read the textbox after the user submit the button inside the function as shown below :



                    var first_name = [];
                    var last_name = [];

                    var fInput;
                    var lInput;
                    var messageBox;

                    function insert ( )
                    fInput = document.getElementById("fname");
                    lInput = document.getElementById("lname");
                    messageBox = document.getElementById("display");
                    first_name.push( fInput.value );
                    last_name.push( lInput.value );

                    Show();


                    function Show ()
                    //clear messagebox
                    fInput.value = "";
                    lInput.value = "";

                    //show
                    messageBox.innerHTML = "";
                    messageBox.innerHTML = mergeHandler(first_name,last_name,merge2Single);
                    messageBox.innerHTML = mergeHandler(first_name,last_name,merge2Object);


                    //merges
                    function merge2Single(first, last)
                    var arr=[];
                    arr.push(first);
                    arr.push(last);
                    return arr.join(" ");


                    function mergeHandler(arr1, arr2, func)
                    var i=0;
                    var size = arr1.length;
                    var result=[];
                    for(i=0; i<size; i++)
                    result.push(func(arr1[i], arr2[i]));

                    return result;


                    function merge2Object(first_name, last_name)
                    var arr=[];
                    arr.push(FirstName: first_name,
                    LastName: last_name);

                    return arr;



                    After this fix, you get the following result:



                    [object Object]





                    share|improve this answer

























                      1












                      1








                      1







                      You should read the textbox after the user submit the button inside the function as shown below :



                      var first_name = [];
                      var last_name = [];

                      var fInput;
                      var lInput;
                      var messageBox;

                      function insert ( )
                      fInput = document.getElementById("fname");
                      lInput = document.getElementById("lname");
                      messageBox = document.getElementById("display");
                      first_name.push( fInput.value );
                      last_name.push( lInput.value );

                      Show();


                      function Show ()
                      //clear messagebox
                      fInput.value = "";
                      lInput.value = "";

                      //show
                      messageBox.innerHTML = "";
                      messageBox.innerHTML = mergeHandler(first_name,last_name,merge2Single);
                      messageBox.innerHTML = mergeHandler(first_name,last_name,merge2Object);


                      //merges
                      function merge2Single(first, last)
                      var arr=[];
                      arr.push(first);
                      arr.push(last);
                      return arr.join(" ");


                      function mergeHandler(arr1, arr2, func)
                      var i=0;
                      var size = arr1.length;
                      var result=[];
                      for(i=0; i<size; i++)
                      result.push(func(arr1[i], arr2[i]));

                      return result;


                      function merge2Object(first_name, last_name)
                      var arr=[];
                      arr.push(FirstName: first_name,
                      LastName: last_name);

                      return arr;



                      After this fix, you get the following result:



                      [object Object]





                      share|improve this answer













                      You should read the textbox after the user submit the button inside the function as shown below :



                      var first_name = [];
                      var last_name = [];

                      var fInput;
                      var lInput;
                      var messageBox;

                      function insert ( )
                      fInput = document.getElementById("fname");
                      lInput = document.getElementById("lname");
                      messageBox = document.getElementById("display");
                      first_name.push( fInput.value );
                      last_name.push( lInput.value );

                      Show();


                      function Show ()
                      //clear messagebox
                      fInput.value = "";
                      lInput.value = "";

                      //show
                      messageBox.innerHTML = "";
                      messageBox.innerHTML = mergeHandler(first_name,last_name,merge2Single);
                      messageBox.innerHTML = mergeHandler(first_name,last_name,merge2Object);


                      //merges
                      function merge2Single(first, last)
                      var arr=[];
                      arr.push(first);
                      arr.push(last);
                      return arr.join(" ");


                      function mergeHandler(arr1, arr2, func)
                      var i=0;
                      var size = arr1.length;
                      var result=[];
                      for(i=0; i<size; i++)
                      result.push(func(arr1[i], arr2[i]));

                      return result;


                      function merge2Object(first_name, last_name)
                      var arr=[];
                      arr.push(FirstName: first_name,
                      LastName: last_name);

                      return arr;



                      After this fix, you get the following result:



                      [object Object]






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Mar 9 at 4:26









                      Prabin PaudelPrabin Paudel

                      1,29741527




                      1,29741527





















                          0














                          Primarily I can see two issue in your code




                          1. You are loading the script before dom is ready so



                            var fInput = document.getElementById("fname");
                            var lInput = document.getElementById("lname");
                            var messageBox = document.getElementById("display");



                            js will not able to find these elements from the dom



                            To solve this issue load js near closing end of body



                            <body>
                            // rest of html
                            <script src="useMerge.js" type="text/javascript"></script>
                            </body>



                          2. If you want to join the first name and last number array , just concat those two arrays and then use join





                          var first_name = [];
                          var last_name = [];

                          var messageBox = document.getElementById("display");
                          var messageBox2 = document.getElementById("display2");

                          function insert()
                          var fInput = document.getElementById("fname");
                          var lInput = document.getElementById("lname");
                          first_name.push(fInput.value);
                          last_name.push(lInput.value);

                          Show();


                          function Show()
                          //clear messagebox
                          fInput.value = "";
                          lInput.value = "";

                          //show
                          messageBox.innerHTML = "";
                          messageBox2.innerHTML = "";

                          messageBox.innerHTML = mergeHandler(first_name, last_name, merge2Single);
                          messageBox2.innerHTML = JSON.stringify(mergeHandler(first_name, last_name, merge2Object));


                          //merges
                          function merge2Single(first, last)
                          return first_name.concat(last_name).join(' ');


                          function mergeHandler(arr1, arr2, func)
                          return func(arr1, arr2);


                          function merge2Object(first_name, last_name)
                          var arr = [];
                          arr.push(
                          FirstName: first_name,
                          LastName: last_name
                          );

                          return arr;

                          article,
                          aside,
                          figure,
                          footer,
                          header,
                          hgroup,
                          menu,
                          nav,
                          section
                          display: block;

                          <form>
                          <h1>Please enter data</h1>
                          <input id="fname" type="text" placeholder="First Name" />
                          <input id="lname" type="text" placeholder="Last Name" />
                          <input type="button" value="Save" onclick="insert()" />
                          </form>
                          <div id="display"></div>
                          <div id="display2"></div>








                          share|improve this answer



























                            0














                            Primarily I can see two issue in your code




                            1. You are loading the script before dom is ready so



                              var fInput = document.getElementById("fname");
                              var lInput = document.getElementById("lname");
                              var messageBox = document.getElementById("display");



                              js will not able to find these elements from the dom



                              To solve this issue load js near closing end of body



                              <body>
                              // rest of html
                              <script src="useMerge.js" type="text/javascript"></script>
                              </body>



                            2. If you want to join the first name and last number array , just concat those two arrays and then use join





                            var first_name = [];
                            var last_name = [];

                            var messageBox = document.getElementById("display");
                            var messageBox2 = document.getElementById("display2");

                            function insert()
                            var fInput = document.getElementById("fname");
                            var lInput = document.getElementById("lname");
                            first_name.push(fInput.value);
                            last_name.push(lInput.value);

                            Show();


                            function Show()
                            //clear messagebox
                            fInput.value = "";
                            lInput.value = "";

                            //show
                            messageBox.innerHTML = "";
                            messageBox2.innerHTML = "";

                            messageBox.innerHTML = mergeHandler(first_name, last_name, merge2Single);
                            messageBox2.innerHTML = JSON.stringify(mergeHandler(first_name, last_name, merge2Object));


                            //merges
                            function merge2Single(first, last)
                            return first_name.concat(last_name).join(' ');


                            function mergeHandler(arr1, arr2, func)
                            return func(arr1, arr2);


                            function merge2Object(first_name, last_name)
                            var arr = [];
                            arr.push(
                            FirstName: first_name,
                            LastName: last_name
                            );

                            return arr;

                            article,
                            aside,
                            figure,
                            footer,
                            header,
                            hgroup,
                            menu,
                            nav,
                            section
                            display: block;

                            <form>
                            <h1>Please enter data</h1>
                            <input id="fname" type="text" placeholder="First Name" />
                            <input id="lname" type="text" placeholder="Last Name" />
                            <input type="button" value="Save" onclick="insert()" />
                            </form>
                            <div id="display"></div>
                            <div id="display2"></div>








                            share|improve this answer

























                              0












                              0








                              0







                              Primarily I can see two issue in your code




                              1. You are loading the script before dom is ready so



                                var fInput = document.getElementById("fname");
                                var lInput = document.getElementById("lname");
                                var messageBox = document.getElementById("display");



                                js will not able to find these elements from the dom



                                To solve this issue load js near closing end of body



                                <body>
                                // rest of html
                                <script src="useMerge.js" type="text/javascript"></script>
                                </body>



                              2. If you want to join the first name and last number array , just concat those two arrays and then use join





                              var first_name = [];
                              var last_name = [];

                              var messageBox = document.getElementById("display");
                              var messageBox2 = document.getElementById("display2");

                              function insert()
                              var fInput = document.getElementById("fname");
                              var lInput = document.getElementById("lname");
                              first_name.push(fInput.value);
                              last_name.push(lInput.value);

                              Show();


                              function Show()
                              //clear messagebox
                              fInput.value = "";
                              lInput.value = "";

                              //show
                              messageBox.innerHTML = "";
                              messageBox2.innerHTML = "";

                              messageBox.innerHTML = mergeHandler(first_name, last_name, merge2Single);
                              messageBox2.innerHTML = JSON.stringify(mergeHandler(first_name, last_name, merge2Object));


                              //merges
                              function merge2Single(first, last)
                              return first_name.concat(last_name).join(' ');


                              function mergeHandler(arr1, arr2, func)
                              return func(arr1, arr2);


                              function merge2Object(first_name, last_name)
                              var arr = [];
                              arr.push(
                              FirstName: first_name,
                              LastName: last_name
                              );

                              return arr;

                              article,
                              aside,
                              figure,
                              footer,
                              header,
                              hgroup,
                              menu,
                              nav,
                              section
                              display: block;

                              <form>
                              <h1>Please enter data</h1>
                              <input id="fname" type="text" placeholder="First Name" />
                              <input id="lname" type="text" placeholder="Last Name" />
                              <input type="button" value="Save" onclick="insert()" />
                              </form>
                              <div id="display"></div>
                              <div id="display2"></div>








                              share|improve this answer













                              Primarily I can see two issue in your code




                              1. You are loading the script before dom is ready so



                                var fInput = document.getElementById("fname");
                                var lInput = document.getElementById("lname");
                                var messageBox = document.getElementById("display");



                                js will not able to find these elements from the dom



                                To solve this issue load js near closing end of body



                                <body>
                                // rest of html
                                <script src="useMerge.js" type="text/javascript"></script>
                                </body>



                              2. If you want to join the first name and last number array , just concat those two arrays and then use join





                              var first_name = [];
                              var last_name = [];

                              var messageBox = document.getElementById("display");
                              var messageBox2 = document.getElementById("display2");

                              function insert()
                              var fInput = document.getElementById("fname");
                              var lInput = document.getElementById("lname");
                              first_name.push(fInput.value);
                              last_name.push(lInput.value);

                              Show();


                              function Show()
                              //clear messagebox
                              fInput.value = "";
                              lInput.value = "";

                              //show
                              messageBox.innerHTML = "";
                              messageBox2.innerHTML = "";

                              messageBox.innerHTML = mergeHandler(first_name, last_name, merge2Single);
                              messageBox2.innerHTML = JSON.stringify(mergeHandler(first_name, last_name, merge2Object));


                              //merges
                              function merge2Single(first, last)
                              return first_name.concat(last_name).join(' ');


                              function mergeHandler(arr1, arr2, func)
                              return func(arr1, arr2);


                              function merge2Object(first_name, last_name)
                              var arr = [];
                              arr.push(
                              FirstName: first_name,
                              LastName: last_name
                              );

                              return arr;

                              article,
                              aside,
                              figure,
                              footer,
                              header,
                              hgroup,
                              menu,
                              nav,
                              section
                              display: block;

                              <form>
                              <h1>Please enter data</h1>
                              <input id="fname" type="text" placeholder="First Name" />
                              <input id="lname" type="text" placeholder="Last Name" />
                              <input type="button" value="Save" onclick="insert()" />
                              </form>
                              <div id="display"></div>
                              <div id="display2"></div>








                              var first_name = [];
                              var last_name = [];

                              var messageBox = document.getElementById("display");
                              var messageBox2 = document.getElementById("display2");

                              function insert()
                              var fInput = document.getElementById("fname");
                              var lInput = document.getElementById("lname");
                              first_name.push(fInput.value);
                              last_name.push(lInput.value);

                              Show();


                              function Show()
                              //clear messagebox
                              fInput.value = "";
                              lInput.value = "";

                              //show
                              messageBox.innerHTML = "";
                              messageBox2.innerHTML = "";

                              messageBox.innerHTML = mergeHandler(first_name, last_name, merge2Single);
                              messageBox2.innerHTML = JSON.stringify(mergeHandler(first_name, last_name, merge2Object));


                              //merges
                              function merge2Single(first, last)
                              return first_name.concat(last_name).join(' ');


                              function mergeHandler(arr1, arr2, func)
                              return func(arr1, arr2);


                              function merge2Object(first_name, last_name)
                              var arr = [];
                              arr.push(
                              FirstName: first_name,
                              LastName: last_name
                              );

                              return arr;

                              article,
                              aside,
                              figure,
                              footer,
                              header,
                              hgroup,
                              menu,
                              nav,
                              section
                              display: block;

                              <form>
                              <h1>Please enter data</h1>
                              <input id="fname" type="text" placeholder="First Name" />
                              <input id="lname" type="text" placeholder="Last Name" />
                              <input type="button" value="Save" onclick="insert()" />
                              </form>
                              <div id="display"></div>
                              <div id="display2"></div>





                              var first_name = [];
                              var last_name = [];

                              var messageBox = document.getElementById("display");
                              var messageBox2 = document.getElementById("display2");

                              function insert()
                              var fInput = document.getElementById("fname");
                              var lInput = document.getElementById("lname");
                              first_name.push(fInput.value);
                              last_name.push(lInput.value);

                              Show();


                              function Show()
                              //clear messagebox
                              fInput.value = "";
                              lInput.value = "";

                              //show
                              messageBox.innerHTML = "";
                              messageBox2.innerHTML = "";

                              messageBox.innerHTML = mergeHandler(first_name, last_name, merge2Single);
                              messageBox2.innerHTML = JSON.stringify(mergeHandler(first_name, last_name, merge2Object));


                              //merges
                              function merge2Single(first, last)
                              return first_name.concat(last_name).join(' ');


                              function mergeHandler(arr1, arr2, func)
                              return func(arr1, arr2);


                              function merge2Object(first_name, last_name)
                              var arr = [];
                              arr.push(
                              FirstName: first_name,
                              LastName: last_name
                              );

                              return arr;

                              article,
                              aside,
                              figure,
                              footer,
                              header,
                              hgroup,
                              menu,
                              nav,
                              section
                              display: block;

                              <form>
                              <h1>Please enter data</h1>
                              <input id="fname" type="text" placeholder="First Name" />
                              <input id="lname" type="text" placeholder="Last Name" />
                              <input type="button" value="Save" onclick="insert()" />
                              </form>
                              <div id="display"></div>
                              <div id="display2"></div>






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Mar 9 at 4:21









                              brkbrk

                              30k32246




                              30k32246





















                                  0














                                  Try this in this function. Just map the object and joint the strings. By the way, you are overriding the results of your functions by trying to display the text in the same div



                                  Finally use the attribute defer in your <script> tag. Right now your javascript in the browser is running before your html is loaded. Just do this:



                                  <script src="script.js" type="text/javascript" defer></script>



                                  function merge2Object(first_name, last_name)
                                  var arr=[];
                                  arr.push(FirstName: first_name,
                                  LastName: last_name);

                                  console.log(arr);
                                  return arr.map(e => `$e.FirstName $e.LastName`).join(",");






                                  share|improve this answer



























                                    0














                                    Try this in this function. Just map the object and joint the strings. By the way, you are overriding the results of your functions by trying to display the text in the same div



                                    Finally use the attribute defer in your <script> tag. Right now your javascript in the browser is running before your html is loaded. Just do this:



                                    <script src="script.js" type="text/javascript" defer></script>



                                    function merge2Object(first_name, last_name)
                                    var arr=[];
                                    arr.push(FirstName: first_name,
                                    LastName: last_name);

                                    console.log(arr);
                                    return arr.map(e => `$e.FirstName $e.LastName`).join(",");






                                    share|improve this answer

























                                      0












                                      0








                                      0







                                      Try this in this function. Just map the object and joint the strings. By the way, you are overriding the results of your functions by trying to display the text in the same div



                                      Finally use the attribute defer in your <script> tag. Right now your javascript in the browser is running before your html is loaded. Just do this:



                                      <script src="script.js" type="text/javascript" defer></script>



                                      function merge2Object(first_name, last_name)
                                      var arr=[];
                                      arr.push(FirstName: first_name,
                                      LastName: last_name);

                                      console.log(arr);
                                      return arr.map(e => `$e.FirstName $e.LastName`).join(",");






                                      share|improve this answer













                                      Try this in this function. Just map the object and joint the strings. By the way, you are overriding the results of your functions by trying to display the text in the same div



                                      Finally use the attribute defer in your <script> tag. Right now your javascript in the browser is running before your html is loaded. Just do this:



                                      <script src="script.js" type="text/javascript" defer></script>



                                      function merge2Object(first_name, last_name)
                                      var arr=[];
                                      arr.push(FirstName: first_name,
                                      LastName: last_name);

                                      console.log(arr);
                                      return arr.map(e => `$e.FirstName $e.LastName`).join(",");







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Mar 9 at 4:26









                                      Pato SalazarPato Salazar

                                      931719




                                      931719



























                                          draft saved

                                          draft discarded
















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid


                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.

                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073851%2fhtml-and-javascript-how-to-return-user-input%23new-answer', 'question_page');

                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

                                          2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

                                          Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme