Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQUERY copy contents of a textbox to a field while typing

Tags:

jquery

I am trying to copy the contents of a textbox into a div simultaneously while the user is typing. Here is THE CODE ON JSFIDDLE The error that is am facing is, the length of the value copied inside the div is always one less than that of the textbox. what error am i making in the script?

like image 381
Sujit Agarwal Avatar asked May 28 '11 06:05

Sujit Agarwal


People also ask

How to copy textbox value using jQuery?

Displaying (Copying) TextBox value to Label using jQueryvar txtName = $("#txtName"); //Reference the Label. var lblName = $("#lblName"); //Copy the TextBox value to Label.

How copy text from one textbox to another in jQuery?

All one need to do is to bind keyup event on textbox and then copy textbox value to another textbox. Below jQuery code will copy text from txtFirst and copy it to txtSecond. $(document). ready(function() { $('#txtFirst').

How do I copy and paste text in jQuery?

You can simply use jQuery to get the value of the original and paste it into the <textarea> like so: $("#copy"). click(function() { $("#paste").

How can set text to textbox in jQuery?

Answer: Use the jQuery val() Method You can simply use the jQuery val() method to set the value of an input text box.


2 Answers

Use keyup instead.

$("#boxx").keyup(function(event) {
  var stt = $(this).val();
  $("div").text(stt);
});

keypress occurs when the key is pressed down and you want the text transferred when the key is released.

like image 94
Guidhouse Avatar answered Sep 28 '22 01:09

Guidhouse


The keyup and keypress events work for keyboard input, but if one uses the mouse to right-click and paste something into the text box then the value change will not be picked up. You can use bind with the input event to register both keyup and paste events like this:

$("#textbox1").bind('input', function () {
   var stt = $(this).val();
   $("#textbox2").val(stt);
});
like image 31
Rahatur Avatar answered Sep 27 '22 23:09

Rahatur