0

I have a lot more code, so this might not look very clean. I am trying to grab some text out of the email input and put in where the p tag is. It doesn't seem to be working. Can someone assist?

 <form class="modal-content" action="#" style="margin-top:10%;">
   <div class="container">
     <label><b>Email</b></label>
     <input id="emailIn" type="email" placeholder=" example@gmail.com" name="email" required>
     <br/><br/> 
   <div class="clearfix">
     <button type="submit" onclick="myFunction()" class="signupbtn">Sign Up</button>
    </div>
   </div>
 </form>

 <p id="demo"></p>

 <script>
 function myFunction() {
   document.getElementById("emailIn").value
   document.getElementById("demo").innerHTML = text;
 }
 </script>

5 Answers5

2

Do small Changes in your code ,

<script>
    function myFunction() {
        document.getElementById("demo").value = document.getElementById("emailIn").value;
    }
Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118
1

The problem is that you are just trying to get the data with text (which isn't declared anywhere) instead of the value of the input.

You could have had the line be:

var text = document.getElementById("emailIn").value;

And then your code would have worked, but since you are only going to use that information just one time, making a variable for storage of the data doesn't make good sense. Instead, you can just get the value and assign it all in one line.

Also, don't use inline HTML event attributes (onclick). Keep all your JavaScript completely separate from your HTML and use modern standards for setting up event handlers. Here's why.

Lastly, .innerHTML causes the string of data you provide to be parsed by the HTML parser. Since the value you are working with won't contain any HTML, it's better to use .textContent, which doesn't process the string as HTML. It's a bit better on performance to use the appropriate one.

// Get reference to the submit button
var btn = document.querySelector(".signupbtn");

// Modern DOM standard for setting up event listeners
btn.addEventListener("click", myFunction);

function myFunction() {  
   // Use textContent instead of innerHTML when there won't be any HTML
   document.getElementById("demo").textContent = document.getElementById("emailIn").value;
 }
<form class="modal-content" action="#" style="margin-top:10%;">
   <div class="container">
     <label><b>Email</b></label>
     <input id="emailIn" type="email" placeholder=" example@gmail.com" name="email" required>
     <br/><br/> 
   <div class="clearfix">
     <button type="submit" class="signupbtn">Sign Up</button>
    </div>
   </div>
 </form>

 <p id="demo"></p>
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
1

You didn't declare text or set it to the value of the input

 function myFunction() {
   var text = document.getElementById("emailIn").value
   document.getElementById("demo").innerHTML = text;
 }
Musa
  • 96,336
  • 17
  • 118
  • 137
0

you almost had it! just define text in your function

function myFunction() {
    text=document.getElementById("emailIn").value
   document.getElementById("demo").innerHTML = text;
 }
<form class="modal-content" action="#" style="margin-top:10%;">
   <div class="container">
     <label><b>Email</b></label>
     <input id="emailIn" type="email" placeholder=" example@gmail.com" name="email" required>
     <br/><br/> 
   <div class="clearfix">
     <button type="submit" onclick="myFunction()" class="signupbtn">Sign Up</button>
    </div>
   </div>
 </form>

 <p id="demo">xxxx</p>

 
DCR
  • 14,737
  • 12
  • 52
  • 115
0

I do things like this thusly:

<form class="modal-content" action="#" style="margin-top:10%;">
 <div class="container">
  <label><b>Email</b></label>
   <input id="emailIn" type="email" placeholder=" example@gmail.com" name="email" required>
  <br/><br/>
  <div class="clearfix">
    <button type="submit" class="signupbtn" id="button">Sign Up</button>
  </div>
  </div>
 </form>

<p id="demo"></p>

<script>
   var button = document.getElementById('button');
   var input = document.getElementById('emailIn');

   button.addEventListener('click', function(){
    var addToP = input.value;
    document.getElementById('demo').innerHTML = demo.innerHTML + '<p>' + addToP + '</p>';
    input.value = '';
  })
</script>
Alan Tweedie
  • 317
  • 2
  • 5
  • That's great that you do things thusly, but you didn't explain what the problem was or why your code solves the problem. That's what a good answer should do. – Scott Marcus Jun 27 '17 at 17:44