-1

What i want to do is that when i click on the button "1" it should display the number "1" in the textbox above it.

what is wrong with my jquery code?

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JQuery Tutorial</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
    <input id="textbox" type="text" />

    <div>
        <input id="1" type="button" value="1" />
        <input id="2" type="button" value="2" />
        <input id="3" type="button" value="3" />
    </div>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"> </script>
    <script type="text/javascript" src="js/ext.js"></script>
</body>

My jquery

$('#1').click(function)({
  var txt = $('#textbox');
  $('#1').html('txt');
});
Eric Herlitz
  • 25,354
  • 27
  • 113
  • 157
what
  • 11
  • 1
  • There is actually no jQuery code ... – thibauts Jul 09 '13 at 10:44
  • 1
    Never give "integer values" to id's! Read more on: http://stackoverflow.com/questions/70579/what-are-valid-values-for-the-id-attribute-in-html and http://stackoverflow.com/questions/1696864/naming-class-and-id-html-attributes-dashes-vs-underlines –  Jul 09 '13 at 10:44

5 Answers5

3

Try this code

 $('input[type="button"]').click(function() {    
       var txt = $(this).val();
       $('#textbox').val(txt);    
 });

Will explain your problem with your jQuery code:

Line 1: You registered with #1 button so this will be triggered on when button 1 is click, I made it generalised

Line 2: You are getting value from textbox, whereas you are supposed to take value from button text.

Line 3: You are assigning value in button, whereas you are supposed to assign to textbox and always use val() for input type of control and html() for other html container controls

The above were your errors explained.

Queries welcomed!

Fiddle demo

Chirag Vidani
  • 2,519
  • 18
  • 26
  • can i ask you something? this maybe pushing it too far, but would you mind helping me learn jquery and programming in general. I can be available whenever you want and we can go over different topics. It would help you get experience of teaching if that is something you want to do, and i can even pay not hourly ( i am still at school but have an interest in programming) but maybe a small amount monthly. – what Jul 09 '13 at 11:05
  • @what I too conduct teaching my existing company and private coaching in past during college :). Can you please contact me on my email (chiragvidani@gmail.com)? – Chirag Vidani Jul 09 '13 at 12:26
0
$(function(){
 $('input[type="button"]').click(function(){
  $('#textbox').val($(this).val());

 });
});
Awais Qarni
  • 17,492
  • 24
  • 75
  • 137
0

You have to get the value using val() method. Also, you are passing variable txt as string to the html() function which is wrong. Try the below...

var txt = $('#textbox').val();

$('#input1').html(txt);
mohkhan
  • 11,925
  • 2
  • 24
  • 27
0

Try this

 $('input[type="button"]').click(function() {
   $('#textbox').val($(this).val());
 });
Ayyappan Sekar
  • 11,007
  • 2
  • 18
  • 22
0

You can try this one

$('div').on('click', 'input', function () {
    $('#textbox').val(this.value);
});