SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: keypress()

  1. #1
    SitePoint Zealot ameerulislam's Avatar
    Join Date
    Jul 2011
    Posts
    145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    keypress()

    I'm trying to produce the same value (for now) when I input something in one text I want it to show up in the other text box, but right now it's showing one less value.. this is the present code
    Code:
    <script>
    	$('#effective').keypress(function(){ 	
    		 var value1 = $(this).val();	 
    		 $('#nominal').attr('value',value1)
    	});
    </script>
    This is what it produces, one less digit,
    keypress1Less.JPG

    then when I start to remove texts it the second text box doesn't adjust values accordingly. It remains what it was.
    keypressNotremoving.JPG

    if keypress() is not a good option to serve this purpose, please let me know what would be.. Thanks for reading and help.

  2. #2
    SitePoint Zealot ameerulislam's Avatar
    Join Date
    Jul 2011
    Posts
    145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    okay I think keyup() is the solution for now.

    I also changed

    $('#nominal').attr('value',value1)
    to
    $('#nominal').val(value1);

    the first one adds value to the value attribute but don't know where the value gets preserved for the second one.

  3. #3
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    From the screenshot, it looks like your trying to sync inputs of the type="number".

    If so, this should work:

    Code:
    <!DOCTYPE HTML>
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Sync input</title>
      </head>
      
      <body>
        <input id="one" type="number" />
        <input id="two" type="number" />
        
        <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
        <script>
          function syncBox(){
            $("#two").val($("#one").val());
          }
          
          $("#one").on({
            keyup: syncBox,
            change: syncBox
          });
        </script>
      </body>
    </html>
    You have to respond to the change event for when you click the arrows to increase and decrease the value.
    You have to respond to the keyup event for when someone is typing in the box.

    HTH


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •