Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how to create a textbox where users enter a value that will change the result of another text below it.

example
HTML
<form>
<input type="text" value="">
<input type="button" value="Apply" >
</form>
<p>The Value is <value></p>

when the apply button is clicked the <value> will print the value provided by user.
Posted
Updated 1-May-15 5:55am
v2
Comments
ZurdoDev 1-May-15 12:00pm    
Just google for some javascript example of how to get text value and how to get text value. Quite easy.
John C Rayan 1-May-15 12:05pm    
Your title is confusing because all you have to do is get the value of textbox using document.getElementById("id_of_textbox").value and assign to the target element. Where do you want to change the value as suggested by your title.

The complete solution is as following.

HTML
<html>
<title>Get Value</title>

<script type="text/javascript">
function ShowValue() {
    document.getElementById('value').innerHTML = document.getElementById("txtBox").value;
}
</script>
<body>

<form>
<input type="text" value="" id="txtBox">
<input type="button" value="Apply" onclick="ShowValue()">
</form>
<p>The Value is <span id="value"></span></p>

</body>
</html>
 
Share this answer
 
Solution 1 explains what you can do. A convenient way for such manipulation would be using jQuery.

First, you can get the jQuery wrappers of the HTML elements using selectors:
http://api.jquery.com/category/selectors[^].

For example, given the id attributes from Solution 1, you can get
C#
var input = $("#inputTxt");
var button = $("#applyBtn");
var span = $("#value");

Then you can add an event handler and use jQuery .val() to read and write value on button click.
JavaScript
button.click(function(){
    var value = input.val();
    span.val(value);
});

Please see:
https://api.jquery.com/click[^],
http://api.jquery.com/val[^].

If you need to learn jQuery (highly recommended), please see:
http://en.wikipedia.org/wiki/JQuery,
http://jquery.com,
http://learn.jquery.com,
http://learn.jquery.com/using-jquery-core,
http://learn.jquery.com/about-jquery/how-jquery-works (start from here).

—SA
 
Share this answer
 
Comments
Thomas Daniels 1-May-15 12:30pm    
+5
Sergey Alexandrovich Kryukov 1-May-15 12:31pm    
Thank you.
—SA
This should work for you.
XML
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<script language="JavaScript">
function ShowMessage() {
    var message =  document.getElementById("text").value;
    document.getElementById('display').innerHTML = "You entered: " + message;
}
</script>
</head>
<body>
<form>
Enter: <input type="text" name="message" id="text">
</form>
<input type="submit" onclick="ShowMessage();"><br />
<span id="display"></span>
</body>
</html>
 
Share this answer
 
Comments
jgakenhe 1-May-15 12:28pm    
Why was my solution down voted? It does exactly what the question asks! Thanks alot.
jgakenhe 1-May-15 12:33pm    
Thank you for the upvote, that's cool. Unfortunately, a few of us had solutions at the same time.
You have to use JavaScript[^] for that.


  1. First, wrap the <value> in a span tag and give IDs to the elements. That way you can easily access them from JavaScript:
    HTML
    <form>
    <input type="text" value="" id="inputTxt">
    <input type="button" value="Apply" id="applyBtn">
    </form>
    <p>The Value is <span id="value">[value]</span></p>

  2. Then, add a <script> tag to your head tag, and put the JavaScript code in there. The code will add a click event handler to your button, and when the button is clicked, it will fetch the text from the textbox and put it in the span in the paragraph:
    HTML
    <script type="text/javascript">
    window.onload = function() {
        document.getElementById("applyBtn").onclick = function() {
            var txt = document.getElementById("inputTxt").value;
            document.getElementById("value").textContent = txt;
        };
    };
    </script>


More information on the used methods in the JavaScript code:
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-May-15 12:23pm    
5ed. Please see Solution 3 for shorter jQuery variant of implementation.
—SA
Member 11657400 2-May-15 2:33am    
Hey thanks for the solution ! Im kinda noob in this started only 3 days ago. Thanks so much !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900