Add a default input box that can change the other to same values

551 views javascript
3

I am looking to have one input that can change all the others input to the same value. I found some example the problem with it is that regardless of any box I type in it changes all the other input boxes to the same. I only want one box that can change all the others. Is there any way to do this? Also here is a JS Fiddle link

$(function(){
  $('.inpt').on('keyup', function(){
    var val = $(this).val();
    $('.inpt').val(val);
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type ="text" class="inpt" />
    <input type ="text" class="inpt" />
    <input type ="text" class="inpt" />
    <input type ="text" class="inpt" />

answered question

1 Answer

9

Just set a different class to the box you only want to change value from:

$(function(){
   $('.inpt-primary').on('keyup', function(){
      var val = $(this).val();
      $('.inpt').val(val);
   })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> 
</script>
<input type ="text" class="inpt inpt-primary" />
<input type ="text" class="inpt" />
<input type ="text" class="inpt" />
<input type ="text" class="inpt" />

posted this

Have an answer?

JD

Please login first before posting an answer.