[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Simple JavaScript form question

Posted on 2016-09-07
4
Medium Priority
?
106 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 59

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
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 …
Suggested Courses

656 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