Solved

Simple JavaScript form question

Posted on 2016-09-07
4
85 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 56

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Jquery driving me nuts... 14 28
Link Anchor Not working 9 51
Table style position to the left 8 26
What is the correct way to check for empty value in input field 2 20
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

726 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