I want the value of my button to appear in a textbox upon clicking the button

5k views Asked by At

I am creating an onscreen keyboard, and want a function which will allow when any of the buttons are pressed, for their values to appear in a text box to the side of the keyboard. The code I have so far is:-

<script type="text/javascript">
    function onclick(){
    document.getElementById("output").value
    =document.getElementById("Q").value;  
    }
</script>

And the HTML code below:-

<div class ="Row">
    <div class="Box2">
      <form id="keyboard" name="keyboard">
        <div>
        <input type="button" onclick='onclick' id="Q" value="Q">
        <input type="button" value="W">
        <input type="button" value="E">
        <input type="button" value="R">
        <input type="button" value="T">
        <input type="button" value="Y">
        <input type="button" value="U">
        <input type="button" value="I">
        <input type="button" value="O">
        <input type="button" value="P">
        </div>
        <div>
        <input type="button" value="A">
        <input type="button" value="S">
        <input type="button" value="D">
        <input type="button" value="F">
        <input type="button" value="G">
        <input type="button" value="H">
        <input type="button" value="J">
        <input type="button" value="K">
        <input type="button" value="L">
        </div>
        <div>
        <input type="button" value="Z">
        <input type="button" value="X">
        <input type="button" value="C">
        <input type="button" value="V">
        <input type="button" value="B">
        <input type="button" value="N">
        <input type="button" value="M">
        </div>
        <div>
        <input type="button" value="SPACE">
        <input type="button" value="ENTER">
        </div> 
      </form>
      <input type='text' id='output' />
    </div>
  </div>
</div>
5

There are 5 answers

0
iCollect.it Ltd On

You have it tagged as jQuery, so here is a more elegant jQuery solution: Throw that code away and use a single delegated jQuery event handler. Start with something like this:

$('[name=keyboard]').on('click', 'input[type=button]', function(){
   var value = $(this).attr('value');
   $('#output').val($('#output').val() + value);
});

JSFiddle: https://jsfiddle.net/TrueBlueAussie/kaau601u/

Obviously you need to handle the SPACE and ENTER as special cases, but you gave no clues what you are doing next, so leaving that to the reader to finish :)

Notes:

  • You either need to place this code after the elements it references or put it in a DOM ready handler.

Like this:

$(function(){
    $('[name=keyboard]').on('click', 'input[type=button]', function(){
       var value = $(this).attr('value');
       $('#output').val($('#output').val() + value);
    });
});
  • Or you can use a document attached handler, which is always present:

Like this:

 $(document).on('click', 'input[type=button]', function(){
    var value = $(this).attr('value');
    $('#output').val($('#output').val() + value);
 });
2
Dennisrec On

//Please try this working example

$('#Q').click(function(){
  $('#output').val($(this).val());
  console.log($(this).val());
 })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class ="Row">
 <div class="Box2">
  <form name=keyboard name="keyboard">
   <div>
    <input type="button" id="Q" value="Q">
    <input type="button" value="W">
    <input type="button" value="E">
    <input type="button" value="R">
    <input type="button" value="T">
    <input type="button" value="Y">
    <input type="button" value="U">
    <input type="button" value="I">
    <input type="button" value="O">
    <input type="button" value="P">
   </div>
   <div>
    <input type="button" value="A">
    <input type="button" value="S">
    <input type="button" value="D">
    <input type="button" value="F">
    <input type="button" value="G">
    <input type="button" value="H">
    <input type="button" value="J">
    <input type="button" value="K">
    <input type="button" value="L">
   </div>
   <div>
    <input type="button" value="Z">
    <input type="button" value="X">
    <input type="button" value="C">
    <input type="button" value="V">
    <input type="button" value="B">
    <input type="button" value="N">
    <input type="button" value="M">
   </div>
   <div>
    <input type="button" value="SPACE">
    <input type="button" value="ENTER">
   </div>
  </form>

  <input type='text' id='output' />
 </div>
</div>

0
Sumesh S On

I have made a slight change in the output field type with working SPACE and ENTER

jQuery('form[name="keyboard"]  input[type="button"]').click(function(){
  
  var inpVal = jQuery(this).val();
  
  if( inpVal == "SPACE" ){
    inpVal = ' '; 
  }else if( inpVal == "ENTER" ){
    inpVal = '\n'; 
  }

  var val = jQuery('#output').val() + inpVal;
  
  
  jQuery('#output').val(val);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>


<div class ="Row">
    <div class="Box2">
      <form name=keyboard name="keyboard">
        <div>
        <input type="button" onclick='onclick' id="Q" value="Q">
        <input type="button" value="W">
        <input type="button" value="E">
        <input type="button" value="R">
        <input type="button" value="T">
        <input type="button" value="Y">
        <input type="button" value="U">
        <input type="button" value="I">
        <input type="button" value="O">
        <input type="button" value="P">
        </div>
        <div>
        <input type="button" value="A">
        <input type="button" value="S">
        <input type="button" value="D">
        <input type="button" value="F">
        <input type="button" value="G">
        <input type="button" value="H">
        <input type="button" value="J">
        <input type="button" value="K">
        <input type="button" value="L">
        </div>
        <div>
        <input type="button" value="Z">
        <input type="button" value="X">
        <input type="button" value="C">
        <input type="button" value="V">
        <input type="button" value="B">
        <input type="button" value="N">
        <input type="button" value="M">
        </div>
        <div>
        <input type="button" value="SPACE" >
        <input type="button" value="ENTER">
        </div> 
      </form>
      <textarea id="output"  cols="40" rows="5"></textarea>
    </div>
  </div>
</div>

0
Dennisrec On

This might help

//this this the script that makes it happen...
 $('form[name=keyboard]>div>input[type=button]').click(function(){
  $('#output').val($(this).val());
  console.log($(this).val());
 })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class ="Row">
 <div class="Box2">
  <form name="keyboard">
   <div>
    <input type="button" value="Q">
    <input type="button" value="W">
    <input type="button" value="E">
    <input type="button" value="R">
    <input type="button" value="T">
    <input type="button" value="Y">
    <input type="button" value="U">
    <input type="button" value="I">
    <input type="button" value="O">
    <input type="button" value="P">
   </div>
   <div>
    <input type="button" value="A">
    <input type="button" value="S">
    <input type="button" value="D">
    <input type="button" value="F">
    <input type="button" value="G">
    <input type="button" value="H">
    <input type="button" value="J">
    <input type="button" value="K">
    <input type="button" value="L">
   </div>
   <div>
    <input type="button" value="Z">
    <input type="button" value="X">
    <input type="button" value="C">
    <input type="button" value="V">
    <input type="button" value="B">
    <input type="button" value="N">
    <input type="button" value="M">
   </div>
   <div>
    <input type="button" value="SPACE">
    <input type="button" value="ENTER">
   </div>
  </form>

  <input title="keyboard" type='text' id='output' />
 </div>
</div>

0
Claus On

Using just javaScript and no need for jQuery.

This example including the use of the SPACE and ENTER keys. As a bonus I added a BACKSPACE key.

Note your output text field was changed to text area to allow for the use of the enter key.

Create a single event listener for the form. This can be done using document query selector.

Capture the click event with the query selector.

Note on the <body> tag we add the onload event handler so when the document is served up the event listener is assigned to the <form> node.

Any click inside the <form> will be tested

HTML and javaScript

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html lang="en">
 <head>
  <script type="text/javascript">
   var g = {};
   function assignListener()
   {
     /*event listener for keyboard*/
      g.keyboard = document.querySelector("#keyboard");
      g.keyboard.addEventListener("click", addToTextBox,false);
   }
   
   function addToTextBox(e)
   {
                /*
                  with query selector the event (e) is passed along
                  to the function.  You can examine the event and get
                  all kinds of useful stuff from it, like type of event, where it came from, attributes like id, class list etc.
                */
    if (e.target.type == 'button')
    {
     switch (e.target.value)
     {
      case "ENTER":
       document.getElementById('output').value += '\n';
       break;
      case "SPACE":
       document.getElementById('output').value += ' ';
       break;
      case "BACKSPACE":
       document.getElementById('output').value = document.getElementById('output').value.substr(0,document.getElementById('output').value.length-1);
       break;
      default:
       document.getElementById('output').value += e.target.value;
     }
    }

   }
  </script>   
 </head>
 
 <body onload="assignListener();">
  <div class ="Row">
   <div class="Box2">
    <form id=keyboard>
                    <div>
                  <input type="button" value="Q">
                  <input type="button" value="W">
                  <input type="button" value="E">
                  <input type="button" value="R">
                  <input type="button" value="T">
                  <input type="button" value="Y">
                  <input type="button" value="U">
                  <input type="button" value="I">
                  <input type="button" value="O">
                  <input type="button" value="P">
                 </div>
                 <div>
                  <input type="button" value="A">
                  <input type="button" value="S">
                  <input type="button" value="D">
                  <input type="button" value="F">
                  <input type="button" value="G">
                  <input type="button" value="H">
                  <input type="button" value="J">
                  <input type="button" value="K">
                  <input type="button" value="L">
                 </div>
                 <div>
                  <input type="button" value="Z">
                  <input type="button" value="X">
                  <input type="button" value="C">
                  <input type="button" value="V">
                  <input type="button" value="B">
                  <input type="button" value="N">
                  <input type="button" value="M">
                 </div>   
           <div>
      <input type="button" value="SPACE">
      <input type="button" value="ENTER">
      <input type="button" value="BACKSPACE">
     </div> 
    </form>
     <!-- <input type='text' id='output' /> -->
      <textarea id="output" rows="5" cols="75"></textarea>
   </div>
  </div> 
 </body>
</html>