0

I'm just trying to make a simple javascript form where everytime you type and submit something, it shows up in the page.

<form id="myForm">
   <input id="text" type="text" name="name" value="">
   <input id="submit" type="submit">
</form>

javascript:

 document.getElementById("myForm").addEventListener('submit', function(){

        var input = document.getElementById("text")
        var output = input.value;

        var printOutput = document.createElement('h1');
        printOutput.innerHTML = output;
        document.body.appendChild(printOutput);

 });    

This shows up for a second then disappears. I understand it's happening because the dom manipulation is happening inside the submit event. But I'm not sure how to go around that.

My first instinct was to use

return output;

then reference the whole function once I appendChild from outside it. But that didn't work either. I'm guessing cause It's an Eventlistener and not a normal function... any ideas on how to go with this?

Magdi Gamal
  • 681
  • 1
  • 9
  • 25

5 Answers5

1

Add return false; at the end of the event listener. This stops the submit from actually happening which refreshes the page

CaptainCarl
  • 3,411
  • 6
  • 38
  • 71
1

Try this instead:

   document.getElementById("myForm").addEventListener('submit', function(event){
            event.preventDefault();

            var input = document.getElementById("text")
            var output = input.value;

            var printOutput = document.createElement('h1');
            printOutput.innerHTML = output;
            document.body.appendChild(printOutput);

     });    
Ivan Minakov
  • 1,432
  • 7
  • 12
1

You are ignoring the form primordial sense that is to send data over a server.

You don't need aform for what you intend. you need only input elements and a handler on the button.

function handler (){ 

  var input = document.getElementById("text")
  var output = input.value;

  var printOutput = document.createElement('h1');
  printOutput.innerHTML = output;
  document.body.appendChild(printOutput);
   document.getElementById("text").value='';

}

document.getElementById("submit").addEventListener('click', handler);
   document.getElementById("text").addEventListener('keypress', function(e){ (e.charCode == 13) && handler();});
<div id="myForm">
  <input id="text" type="text" name="name" value="">
  <input id="submit" type="submit">
</div>
bluehipy
  • 2,254
  • 1
  • 13
  • 19
1

the simplest way of getting input and display on the same page is

function display()
{
var input=document.getElementById("text").value;
document.getElementById("display").innerHTML=input;
}
<form id="myForm">
   <input id="text" type="text" name="name" value="">
   <input id="submit" type="button" onclick=display() value="submit">
   <div id="display"></div>
</form>

if you want to display multiple inputs i.e. each input entered by the user.

document.getElementById("display").innerHTML += input+"<br>";

Note: Always try to write easiest and shortest code for the optimized result.

gourav bajaj
  • 170
  • 10
  • Thanks! That also worked and you're right it looks so much cleaner with the event in the html instead. Although now I'm trying to fix that the enter key doesn't 'submit' the value anymore – Magdi Gamal Aug 17 '17 at 10:28
  • Refer : https://stackoverflow.com/questions/905222/enter-key-press-event-in-javascript accept the answer if u are satisfied. – gourav bajaj Aug 17 '17 at 10:50
1

yes it behaves how it should. it's form and what it does it submits all information to new page. you didn't provide action='' parameter and means it sends information on same page. when you click submit what happens is first you append h1 element and then it reloads, that's why it disappears. if you don't have other page to submit information get rid of forms.

<input id="text" type="text" name="name" value="">
<input id="submit" onclick='return getoutup();'  type="submit">
<h1 id='output'></h1>

<script type="text/javascript">

function getoutup(){
document.getElementById('output').innerText=document.getElementById('text').value;
return false;
}

</script>

so you see that i have pre-created h1 tag, so you don't need much coding to append as function goes. just pre create with value of none and then change with one line of code. hope it helps. maybe it's not correct answer kind of changed your way.

gourav bajaj
  • 170
  • 10