Solved

dynamically changing value of input field

Posted on 2011-02-17
5
447 Views
Last Modified: 2012-05-11
Hi,
a newbie will appreciate your help!

lets say that i have an html form with two text input fields that the user fills and another readonly input field. i wish that using js, the readonly field will always show, lets say, a concatenation of the two other fields. generally the question is how do i use the onblur/onclick etc. methods in certain fields to dynamically change the VALUE of another field.

thanks!

0
Comment
Question by:Or_A
[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
5 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 34918542
0
 
LVL 20

Expert Comment

by:Proculopsis
ID: 34918582

Try something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_26829060.html</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">

jQuery(document).ready( function () {

  $(".concat").keyup( function() {
    var result = "";
    $(".concat").each( function() {
      result += $(this).val();
    });
    $("#result").val( result );
  });

});

</script>
<body>

<input class="concat" /> + <input class="concat" /> = <input id="result" readonly="true" />
 
</body>
</html>

Open in new window

0
 
LVL 54

Accepted Solution

by:
Huseyin KAHRAMAN earned 500 total points
ID: 34918881
using pure js
<script type="text/javascript">
function concat(){
	var result = document.getElementById("result");
	var txt1 = document.getElementById("txt1");
	var txt2 = document.getElementById("txt2");
	result.value=txt1.value+ txt2.value;
	}
</script>
<body>

<input id=txt1 class="concat" onKeyPress="concat()"/> + <input id=txt2 class="concat" onKeyPress="concat()"/> = <input id="result" readonly="true" />

Open in new window

0
 

Author Closing Comment

by:Or_A
ID: 34924607
This is what i wanted, thanks, but i have two small questions:
1. i changed "OnKeyPress" to "OnKeyUp", because the first one was not working well, it always reflected in the "result" only the first n-1 characters from the last input that had been updated.  "OnKeyUp" works great.
2. what is the:
<input...>+<input...>=<input...> for?
i just used:
<input...>
<input...>
<input...>
can you please explain?
thanks :)
0
 
LVL 54

Expert Comment

by:Huseyin KAHRAMAN
ID: 34926423
I just copied the sample from previous jQuery solution...

<input...>+<input...>=<input...>

you can ignore it...  OnKeyUp & OnKeyPress is almost same, whicever works fine for you, use that one...

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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 demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

734 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