Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

JQuery get textbox value dynamically

Posted on 2012-04-10
15
Medium Priority
?
462 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
[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
  • 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 1000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 1000 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

618 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