Reputation: 2198
I'm new with javascript so hopefully what I want would work. I checked a few forums and tutorials and most of them only teaches things like
<p>Original name: <span id="origname"></span></p>
<p>New name: <span id="newname"></span></p>
<script>
var employees = [
{ "firstName" : "John" , "lastName" : "Doe" },
{ "firstName" : "Anna" , "lastName" : "Smith" },
{ "firstName" : "Peter" , "lastName" : "Jones" }, ];
document.getElementById("origname").innerHTML=employees[0].firstName + " " + employees[0].lastName;
// Set new name
employees[0].firstName="Gilbert";
document.getElementById("newname").innerHTML=employees[0].firstName + " " + employees[0].lastName;
</script>
which is pretty understandable but I just want to do something as simple as input a text field then submit and within the same page at the bottom will show whatever the text is being inputted.
I wonder if anyone can give me a hand with this.
EDITED
This is what I have at the moment after getting the replies from all the nice people and the script makes sense to me but somehow it's still not working though :(
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="jquery-1.9.1.min.js"></script>
</head>
<body>
<form action=" " method="post">
<input id="myInput" type="text"/>
<input id="myButton" type="button" value="submit"/>
<p id="myOutput"></p>
</form>
<script>
$("#myButton").click(function() {
var data = $("#myInput").val();
$("#myOutput").val(data);
});
</script>
</body>
additional question out of the blue. let's say at the moment my code is
<p id="myOutput" style="color: yellow; background: black; width: 100px;"></p>
is jquery able to change the codes? let's say if somehow I type red then the code would be
<p id="myOutput" style="color: red; background: black; width: 100px;"></p>
and if I have another input text area and input 200 then the code would be
<p id="myOutput" style="color: yellow; background: black; width: 200px;"></p>
something like that....
Upvotes: 1
Views: 591
Reputation: 5598
Based on the picture you supplied, I would approach this using AJAX for the data submission and then jQuery to update/reflect a <div>
on the page (#3 in your pic).
You might already know how to do the above but if not, I found these tutorials extremely helpful when learning AJAX and updating div's with jQuery:
Handling a form with AJAX: http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
Then updating a <div>
on your page using the success
function when AJAX is done: http://api.jquery.com/jQuery.ajax/
However, if you don't actually need to process the data...then just use JQUERY to handle all of it! Normal form, then use the below to update the DIV with the data onclick of submit.
<div id="output"></div>
<script>
$("#submit").click(function() {
var output = $("#new_name").val();
$("#output").text(output);
});
</script>
UPDATED
Thanks for posting the code. I fiddled a working solution here http://jsfiddle.net/hwGde/4/
And here is the code with the comment on the line that had the issue:
<form action=" " method="post">
<input id="myInput" type="text" />
<input id="myButton" type="button" value="submit" />
<p id="myOutput"></p>
</form>
<script>
$("#myButton").click(function () {
var data = $("#myInput").val();
// $("#myOutput").val(data); this should be .text instead of .val
$("#myOutput").text(data);
});
</script>
Let me know how this works. And don't forget to mark it as the answer (if it works) for future users. Thanks!
Upvotes: 1
Reputation: 2685
If you use jquery to write less do more,
<input id="myInput" type="text"/>
<input id="myButton" type="button"/>
<p id="myOutput"></p>
<script>
$("#myButton").click(function() {
var data = $("#myInput").val();
$("#myOutput").val(data);
});
</script>
Additionally take a look at knockout, you can do such things easier when there are lots of situations like yours in a page.
If you use knockout
<input type="text" data-bind="value:data"></input>
<p data-bind="text:data"></p>
<script>
function AppViewModel() {
this.data= ko.observable("");
}
ko.applyBindings(new AppViewModel());
</script>
Upvotes: 1
Reputation: 11717
you can do this by,
<input type="text" id="name2" onclick="changeName()" />
<script>
function changeName(){
document.getElementById('newname').innerHTML=document.getElementById('name2').value;
}
</script>
Upvotes: 1