1

I'm trying to create a function so that when one of my buttons is pressed the value of the text input changes to the value of the button pressed.

<div id="viewer">
<h1>
<input type="text" id="Screen" maxlength="8" value="0"/>
</h1>

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

This is the function I am trying to use but am going wrong somewhere:

$(document).ready(function(){

$("button").click(function(){

   var $val = $("#Display").val();
    alert($val);
       onclick="sevenClick()";

});
});
});

4 Answers 4

1
$(document).ready(function() {
    $('button').click(function() {
        var test = $(this).attr('value');
        $('#Screen').val(test);
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

sorry i had to edit it a couple of times, some typing mistakes, try it it works :)
0

I think you changed an ID and need to change a few other things

$(document).ready(function() {

  $("button").click(function() {

    var val = $(this).val()
    $("#Screen").val(val);
    alert(val);
    onclick = "sevenClick()";
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="viewer">
<h1>
<input type="text" id="Screen" maxlength="8" value="0"/>
</h1>

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

Comments

0

Use this to keep the actual button value:

$(document).ready(function() {
  $("button").click(function() {
    var $val = $(this).val();
    //alert($val);
    $('#Screen').val($val);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="viewer">
  <h1>
<input type="text" id="Screen" maxlength="8" value="0"/>
</h1>

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

Comments

0

Simply get the value of button and set it to the input box

$(document).ready(function() {
    $("button").click(function() {
        var butVal = $(this).val(); //get current button value here 
        $("#Screen").text(butVal); // set button value to the textbox
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.