[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 473
  • Last Modified:

JQuery get textbox value dynamically

Hello,
I am trying to get a value out of a textbox and then have it placed in two places on the page mixed with other text and would like to have it dropped there as it is being typed in the textbox. How can that be done?
0
maredzki
Asked:
maredzki
  • 7
  • 5
  • 3
2 Solutions
 
leakim971PluritechnicianCommented:
setTimerout(function() { $("#otherPlaceID1,#otherPlaceID2,,#otherPlaceIDn").val( $("#ID_OF_YOUR_TEXTBOX").val() ) }, 350);

Open in new window


or if you don't worry about all technic to change the textbox value, just use :

$("#ID_OF_YOUR_TEXTBOX").change(function() {
      $("#otherPlaceID1,#otherPlaceID2,,#otherPlaceIDn").val( $(this).val() );
});

Open in new window

or :

$("#ID_OF_YOUR_TEXTBOX").bind("change keyup blur focus copy paste click",function() {
      $("#otherPlaceID1,#otherPlaceID2,,#otherPlaceIDn").val( $(this).val() );
});

Open in new window

0
 
maredzkiAuthor Commented:
leakim971,
any ideas how I can have the labels (i.e. #otherPlaceID1) expand dynamically based on the text that is entered in the textbox?
0
 
leakim971PluritechnicianCommented:
you can play with width when possible with :

$("#otherPlaceID1").css( "width", "100px");
$("#otherPlaceID1").css( "width", $("#ID_OF_YOUR_TEXTBOX").width * 1.25 + "px" ); // 1.25 x width of your textbox

if it's a label, use html() instead val()

$("#otherPlaceID1,#otherPlaceID2,,#otherPlaceIDn").html( $(this).val() );
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
maredzkiAuthor Commented:
ok, actually what is best used in this scenario if not a label if you just want to display text?
0
 
leakim971PluritechnicianCommented:
the best? I don't know. It depend your needs... You can use span, div, p, h1, h2, h3 and so on.
0
 
maredzkiAuthor Commented:
here is the link I am trying to test, take a look. Does not seem to work.

URL
0
 
leakim971PluritechnicianCommented:
<script type="text/javascript">
<script language="JavaScript">

what is this ?

valid  :

<script type="text/javascript" language="Javascript">

now you must be sure to run the code once page is totally loaded not before :

<script type="text/javascript" language="Javascript">

$(document).ready(function() {

$("#txt_name).change(function() {
      $("#txt_name_dynamic1").val( $(this).val() );
});

});

</script>

Open in new window

0
 
maredzkiAuthor Commented:
not sure...

made the changes you recommended, but still no joy.
0
 
leakim971PluritechnicianCommented:
there's a missing quote after text_name, should be :
$("#txt_name").change(function() {
0
 
mcnuteCommented:
use the keydown() method of jquery.

$("#txt_name").keydown(function(e) {

$("#txt_name_dynamic1").val( e.target.val() ); // val() is only for input text fields use html() instead for span div p

});

Open in new window

0
 
maredzkiAuthor Commented:
mcnute,
so for span/div it would look like this?

$("#txt_name").keydown(function(e) {

$("#txt_name_dynamic1").html( e.target.html() ); // val() is only for input text fields use html() instead for span div p

});

Open in new window

0
 
mcnuteCommented:
No, it still would be val(), because your $("#txt_name") must be an input field for users to write something in it.

$("#txt_name").keydown(function(e) {

         $("#txt_name_dynamic1").html*( e.target.val()** ); // val() is only for input text fields use html() instead for span div p

});

* this here can be "val()" or "html()" according to if  $("#txt_name_dynamic1") is an input field or a div, span, p etc.

** this must remain val() in order to get the user input.
0
 
maredzkiAuthor Commented:
Thank you both for helping me out getting this done. Here is a working sample in case someone would like to use it.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>

<script type="text/javascript" language="Javascript">
$(document).ready(function() {

$(function(){
$('#txt_name').keyup(function(){
$('#txt_name_dynamic1,#txt_name_dynamic2').text($(this).val());
});
});

});

</script>

</script>
</head>
<body>

<p class="strong">Enter name</p>
    <p><input type="text" id="txt_name" name="txt_name" size="50">

<p>We should see the value of the textbox  -> <span id="txt_name_dynamic1"></span> <- here and here -> <span id="txt_name_dynamic2"></span> <- </p>

</body>
</html>

Open in new window

0
 
mcnuteCommented:
you're right with keyup, obviously you want the input after the input has changed.
0
 
maredzkiAuthor Commented:
Thanks guys!
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 7
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now