Solved

Simple JavaScript form question

Posted on 2016-09-07
4
52 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
4 Comments
 
LVL 32

Accepted Solution

by:
Big Monty earned 250 total points
Comment Utility
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 51

Assisted Solution

by:Julian Hansen
Julian Hansen earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Scrolling the scrollbar in a list in html 9 22
Register AutoHotkey 12 38
Safari SVG Image Problem 1 19
getting id from database 5 22
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now