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
Solved

JQuery get textbox value dynamically

Posted on 2012-04-10
15
449 Views
Last Modified: 2012-04-11
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
Comment
Question by:maredzki
  • 7
  • 5
  • 3
15 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 37829272
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
 
LVL 2

Author Comment

by:maredzki
ID: 37829369
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
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 250 total points
ID: 37829395
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 2

Author Comment

by:maredzki
ID: 37829404
ok, actually what is best used in this scenario if not a label if you just want to display text?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 37829434
the best? I don't know. It depend your needs... You can use span, div, p, h1, h2, h3 and so on.
0
 
LVL 2

Author Comment

by:maredzki
ID: 37829544
here is the link I am trying to test, take a look. Does not seem to work.

URL
0
 
LVL 82

Expert Comment

by:leakim971
ID: 37829607
<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
 
LVL 2

Author Comment

by:maredzki
ID: 37829661
not sure...

made the changes you recommended, but still no joy.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 37830215
there's a missing quote after text_name, should be :
$("#txt_name").change(function() {
0
 
LVL 11

Expert Comment

by:mcnute
ID: 37831798
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
 
LVL 2

Author Comment

by:maredzki
ID: 37832378
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
 
LVL 11

Accepted Solution

by:
mcnute earned 250 total points
ID: 37832483
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
 
LVL 2

Author Comment

by:maredzki
ID: 37833547
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
 
LVL 11

Expert Comment

by:mcnute
ID: 37833569
you're right with keyup, obviously you want the input after the input has changed.
0
 
LVL 2

Author Closing Comment

by:maredzki
ID: 37833737
Thanks guys!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

839 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