?
Solved

Simple JavaScript form question

Posted on 2016-09-07
4
Medium Priority
?
100 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 1000 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 58

Assisted Solution

by:Julian Hansen
Julian Hansen earned 500 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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

771 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