Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Populating Input Fields Using Button UIs

Populating Input Fields Using Button UIs

Scheduled Pinned Locked Moved JavaScript
javascripthtmltoolshelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ASPnoob
    wrote on last edited by
    #1

    Hi all, I have a web app that has a display box and button UIs for typing numbers (0 through 9) into the display box. There is one button that when clicked generates multiple input fields. My problem is that I can enter numbers into the first input field by clicking the numbered buttons and by typing numbers using the keyboard. Numbers can only be entered into other input fields using the keyboard. Any attempt to use the numbered buttons to enter numbers into other input fields will cause the focus to return back to the first input field. Below are my markups and jquery

    <body>

    <script type="text/javascript">
    $(document).ready(function () {

    if ($('#box1').click())
    {      
        $(".BTN").click(function () {
            if ($.isNumeric($(this).html())){
                $("#box1").val($("#box1").val() + $(this).html());           
        }});    
             
         }
         else
             if ($('#box2').click())
             {                  
                 $(".BTN").click(function () {
                     if ($.isNumeric($(this).html())) {
                         $("#box2").val($("#box2").val() + $(this).html());
                     }
                         
                     });                 
             }
             else
                 if ($('#box3').click())
                 {
                     $(".BTN").click(function () {
                         if ($.isNumeric($(this).html())) {
                             $("#box3").val($("#box3").val() + $(this).html());    
                         }});
                 }
    
        $("#functs").click(function () {
         
       document.getElementById("myOutputs").innerHTML =
        ""
       + ""
            + ""
       + ""
            + ""
       + ""
            + ""
       + "
    

    <input type='text' id='box1'/>

    <input type='text' id='box2'/>

    <input type='text' id='box3'/>

    ";
    document.getElementById('box1').tabIndex = "1";
    document.getElementById('box2').tabIndex = "2";
    document.getElementById('box3').tabIndex = "3";
    });
    }

    Please explain why the code above is giving me the problems I've stated, thanks in advance.

    Richard DeemingR 1 Reply Last reply
    0
    • A ASPnoob

      Hi all, I have a web app that has a display box and button UIs for typing numbers (0 through 9) into the display box. There is one button that when clicked generates multiple input fields. My problem is that I can enter numbers into the first input field by clicking the numbered buttons and by typing numbers using the keyboard. Numbers can only be entered into other input fields using the keyboard. Any attempt to use the numbered buttons to enter numbers into other input fields will cause the focus to return back to the first input field. Below are my markups and jquery

      <body>

      <script type="text/javascript">
      $(document).ready(function () {

      if ($('#box1').click())
      {      
          $(".BTN").click(function () {
              if ($.isNumeric($(this).html())){
                  $("#box1").val($("#box1").val() + $(this).html());           
          }});    
               
           }
           else
               if ($('#box2').click())
               {                  
                   $(".BTN").click(function () {
                       if ($.isNumeric($(this).html())) {
                           $("#box2").val($("#box2").val() + $(this).html());
                       }
                           
                       });                 
               }
               else
                   if ($('#box3').click())
                   {
                       $(".BTN").click(function () {
                           if ($.isNumeric($(this).html())) {
                               $("#box3").val($("#box3").val() + $(this).html());    
                           }});
                   }
      
          $("#functs").click(function () {
           
         document.getElementById("myOutputs").innerHTML =
          ""
         + ""
              + ""
         + ""
              + ""
         + ""
              + ""
         + "
      

      <input type='text' id='box1'/>

      <input type='text' id='box2'/>

      <input type='text' id='box3'/>

      ";
      document.getElementById('box1').tabIndex = "1";
      document.getElementById('box2').tabIndex = "2";
      document.getElementById('box3').tabIndex = "3";
      });
      }

      Please explain why the code above is giving me the problems I've stated, thanks in advance.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      jQuery's .click method[^] has two forms; one to attach a handler to the click event of the matched elements, and one to trigger the click event. Your code is calling the second version. Since the method returns the original jQuery wrapper object, which in Javascript terms is not "false-y", the code within the first if block executes. Try something like this instead:

      $(function() {

      // Store the last box to receive the input focus:
      var currentBox = null;
      $(document).on("focus", "#box1, #box2, #box3", function(){ currentBox = $(this); });

      $(document).on("click", ".btn", function() {
      if (currentBox) {
      var value = $(this).html();
      if ($.isNumeric(value)) {
      currentBox.val(currentBox.val() + value);
      }
      }
      });

      });


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      A 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        jQuery's .click method[^] has two forms; one to attach a handler to the click event of the matched elements, and one to trigger the click event. Your code is calling the second version. Since the method returns the original jQuery wrapper object, which in Javascript terms is not "false-y", the code within the first if block executes. Try something like this instead:

        $(function() {

        // Store the last box to receive the input focus:
        var currentBox = null;
        $(document).on("focus", "#box1, #box2, #box3", function(){ currentBox = $(this); });

        $(document).on("click", ".btn", function() {
        if (currentBox) {
        var value = $(this).html();
        if ($.isNumeric(value)) {
        currentBox.val(currentBox.val() + value);
        }
        }
        });

        });


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        A Offline
        A Offline
        ASPnoob
        wrote on last edited by
        #3

        Hi, thanks for replying. Your code works like a charm.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups