Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the following snippet I try to pass an input value to an array and then to print it to an excisting paragraph.The strange thing is that the value is printed to the paragraph for a second and then it returns to the previous status.



<html>
   <head>
     <title>forms-javascript</title>
	</head>


    <body>
    <p id="prgrph">form-test</p>
    <form id="frm">
	   <label for="input2">Town:</label>
	  <input type="text" name="npt" id="input2" required>
	  <button  id="btn">Submit</button>
	</form>

    <script>
      let myArray=[];      
	  document.querySelector("#btn").addEventListener("click",function(e){
	  e.preventDefault;
	  myArray.push(document.querySelector("#input2").value);
	  document.querySelector("#prgrph").innerText=myArray[0];
	  });
	  
	 </script>
    </body>
    </html>	



Thank you in advance!

What I have tried:

I have been watching tutorials on youtube
Posted
Updated 3-Feb-23 9:04am

1 solution

Try to use the next snippet in the button (click event)

var yourArr = $('#input2').map(function() {
    return $(this).val();
}).get();


the above code will get the "input2" value and append it in an array named (yourArr)

hope it helps
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900