开发者

JQUERY copy contents of a textbox to a field while typing

开发者 https://www.devze.com 2023-03-08 18:33 出处:网络
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

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 o开发者_如何学Cf the value copied inside the div is always one less than that of the textbox. what error am i making in the script?


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.


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);
});


The keypress event occurs before the text in the <input> element is updated. You can delay the copy operation to work around that. Even a 0 millisecond delay will be enough for the copy operation to occur after the element is updated:

$("#boxx").keypress(function() {
    var $this = $(this);
    window.setTimeout(function() {
       $("div").text($this.val());
    }, 0);
});

Updated fiddle here.


$("#title").keypress(function() {
                var $this = $(this);
                window.setTimeout(function() {

                   $("#slug-url").val($this.val().toLowerCase().replace(/ /g, '-'));
                }, 0);
            });

using @Frédéric Hamidi' method to generate seo friendly url after replacing spaces with '-' and changing text to smallcase.


use keyup and change both.

$("#boxx").on('keypress change', function(event) {
       var data=$(this).val();
       $("div").text(data);
});

here is the example http://jsfiddle.net/6HmxM/785/


$("#boxx").keyup(function() {
    var $this= $(this);
    window.setTimeout(function() {
       $("div").text($this.val());
    }, 0);
});

this work proper.

Copy also works f9enter code here

0

精彩评论

暂无评论...
验证码 换一张
取 消