Display selected input value [closed]

0

I have two inputs with their respective ID's, I would like the alert to display the value entered in the selected field, so if I am typing in field 1 it will display the value in the alert referring to field 1 if I am typing in field 2 to display the value in the alert for field 2.

Idea Update

I saw the code from JuniorNunes7 and I had an idea and would like to do something new, sorry. See the image:

Thank you

    
asked by anonymous 11.11.2016 / 12:41

2 answers

0

I think this is what you need according to the description of your image, I just did not put any alerts, but I imagine you were just trying to test ...

$("#principal").on('change keyup paste', function(){
  var input = $('.check:checked').val();
  $("#"+input).val($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div><label>Master</label><inputid="principal" type="text">
</div>
<br />

<div>
  <input type="radio" name="check" class="check" value="campo1" checked>
  <label>campo1</label>
  <input class="campo" id="campo1" name="campo1" type="text" readonly>
</div>

<div>
  <input type="radio" name="check" class="check" value="campo2">
  <label>campo2</label>
  <input class="campo" id="campo2" name="campo2" type="text" readonly>
</div>
    
11.11.2016 / 18:05
1

To show something whenever the input value is changed (typed, deleted, pasted), you can use the onInput event.

$('#input-id').on('input', function() {
    alert($(this).val());
});

It is also possible to use change , it will be fired only when the user "deselects" the input.

$('#input-id').on('change', function() {
    alert($(this).val());
});

See a working example in JSFiddle

    
11.11.2016 / 12:47