Solved

Simple JavaScript form question

Posted on 2016-09-07
4
93 Views
Last Modified: 2016-09-19
This is similiar to a question I asked recently (which was answered, but unfortunately I don't know this material as well as I'd like, hence I have to ask this follow-up question). I'm trying to learn JavaScript. I have very simple form (just one text field and one submit button). I want it so after the user enters material in the text box and clicks the button, his input will appear after the "Your name is" sentence.
     Below is the code, which is not working. I thought I did everything right. I was able to achieve an alert that will pop up with the user's input when the button is clicked, so I think I'm on the right track.
     I'd like to know the JavaScript way to achieve this (getting the user's input to appear after that sentence). If possible, can someone also show me the jQuery way?

<!DOCTYPE html>
<html><head></head>

<body>
<form>
Enter your name <input type="text" id="input">&nbsp;&nbsp;
<input type="submit" onclick="working()"><br><br>
</form>

<p>Your name is<span id="output"></span></p>

<script>
function working() {
var answer = document.getElementById("input").value;
document.getElementById("output").innerHTML = answer;
//alert(answer);
}
</script>

</body></html>

Open in new window

0
Comment
Question by:john8217
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 33

Accepted Solution

by:
Big Monty earned 250 total points
ID: 41788391
since you're using a submit button, it'll seem like it's not working, since it's going to POST the page. what you should do is change

<input type="submit" onclick="working()">

to

<input type="button" onclick="working()">

to use jquery, you could do:

function working() {
var answer = $("#input").val();
$("#output").text( answer );
//alert(answer);
}
0
 
LVL 57

Assisted Solution

by:Julian Hansen
Julian Hansen earned 125 total points
ID: 41788421
That won't work - the onclick needs to return a false value to prevent the submit.

Rather do this
Note the 'return'
<input type="submit" onclick="return working()">

Open in new window

And in your JavaScript
<script>
function working() {
  var answer = document.getElementById("input").value;
  document.getElementById("output").innerHTML = answer;
  return false;
}
</script>

Open in new window

0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 125 total points
ID: 41792548
jQuery version :

<!DOCTYPE html>
<html><head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script>
jQuery(function($) {
     $("#fakeSubmit").click(function(evt) {
            evt.preventDefault(); // prevent submit of the form, use "button" attribute instead "submit" to remove this line
            var answer = $("#input").val();
            $("#output").text(answer);
     });
});
</script>
</head>
<body>
<form>
Enter your name <input type="text" id="input">&nbsp;&nbsp;
<input type="submit" id="fakeSubmit"><br><br>
</form>

<p>Your name is<span id="output"></span></p>
</body></html>

Open in new window

0
 

Author Closing Comment

by:john8217
ID: 41805788
Big Monty and Julian Hansen, both of your (JavaScript) methods works. Thanks.
     Big Monty and leakim971, thank you for showing the jQuery way.
     Thanks to all three of you, and sorry for the delay in responding.
0

Featured Post

Containers and Docker for Everyone

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question