1

How to bind button values from input text box. when we click on the button, button value will be shown in text box, if we change value from input text box have to change selected button value too ..

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>val demo</title>
  <style>
  button {
    margin: 4px;
    cursor: pointer;
  }
  input {
    margin: 4px;
    color: blue;
  }
  </style>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
 
<div>
  <button>Feed</button>
  <button>the</button>
  <button>Input</button>
</div>
<input type="text" value="click a button">
 
<script>
$( "button" ).click(function() {
  var text = $( this ).text();
  $( "input" ).val( text );
});
</script>
 
</body>
</html>

1
  • if we change value from input text box have to change selected button value too how would you differentiate between selected and unselected button? Commented Feb 4, 2016 at 7:50

2 Answers 2

2

Try this

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>val demo</title>
  <style>
  button {
    margin: 4px;
    cursor: pointer;
  }
  input {
    margin: 4px;
    color: blue;
  }
  </style>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
 
<div>
  <button>Feed</button>
  <button>the</button>
  <button>Input</button>
</div>
<input type="text" value="click a button">
 
<script>
var button = $();
$( "button" ).click(function() {
  button = $(this);
  var text = $( this ).text();
  $( "input" ).val( text );
});
$("input").on("input", function(){
    button.html($(this).val());
});
</script>
 
</body>
</html>

Sign up to request clarification or add additional context in comments.

2 Comments

@mylee if 'ínput' is in a page and 'buttons' are in another page.. where binding is not working between them,, then how can we solve this ??
by another page do you mean in frames or iframes?
1

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>val demo</title>
  <style>
  button {
    margin: 4px;
    cursor: pointer;
  }
  input {
    margin: 4px;
    color: blue;
  }
  </style>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
 
<div>
  <button>Feed</button>
  <button>the</button>
  <button>Input</button>
</div>
<input type="text" value="click a button">
 
<script>
var actualButton;
$( "button" ).click(function() {
  actualButton = $(this);
  var text = $( this ).text();
  $( "input" ).val( text );
});
$("input").on('keyup', function(e){
  if(actualButton === undefined)
    return;

  actualButton.text($(this).val());
});
</script>
 
</body>
</html>

Edit: Ohh someone was faster ;)

4 Comments

if 'ínput' is in a page and 'buttons' are in another page.. where binding is not working between them,, then how can we solve this ??
yeah sure,, thanx in advance
sorry,, the two pages added with php,, i con't send with jsfiddle.. is then any hint to bind between one page to another page ??

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.