Ashik Ashok
Ashik Ashok

Reputation: 31

passing the value of button value to textfield

i need to pass the value of button id to the textfield on onclick function,i have some scratch code below,but am new to javascrip. JSP Page B1 B2 B3

   <script type="text/javascript">
   function reply_click(clicked_id)
     {
    alert(clicked_id);
     }
     </script>
    <input type="text" name="get">
   </body>
   </html>

Upvotes: 0

Views: 1536

Answers (3)

Miguel
Miguel

Reputation: 20633

If I understood correctly, you want to send the value of the button (or the button id?) to the input field.

document.querySelector('#button').onclick = function(event) {
  document.querySelector('#textfield').value = this.textContent;
};

document.querySelector('#button-id').onclick = function(event) {
  document.querySelector('#textfield').value = this.id;
};
<button id="button">button value</button>
<button id="button-id">button id</button>

<input type="text" id="textfield">

Upvotes: 1

fiacobelli
fiacobelli

Reputation: 1990

You need to give "get", the text element, an ID and use document.getElementById to manipulate it and change its value.

<html>
<script type="text/javascript">
   function reply_click(clicked_id)
     {
       document.getElementById("btnId").value=clicked_id;
     }
     </script>
    <body>
      <input type="text" name="get" id="btnId">
      <input type="button" id="one" value="Btn 1" onclick="reply_click(this.id);">
      <input type="button" id="two" value="Btn 2" onclick="reply_click(this.id);">
   </body>
   </html>

Upvotes: 0

Dimitris Karagiannis
Dimitris Karagiannis

Reputation: 9358

Let

<button id="some-id">Button</button>

be our button and

<input type='text' id="some-other-id"/>

be our input field

then

var button = document.getElementById('some-id'),
    inputField = document.getElementById('some-other-id');
button.addEventListener('click', function(e) {
    inputField.value = this.id;
});

Upvotes: 0

Related Questions