Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript synchronize inputs with same name

I need to synchronize all inputs with same name. I don't know inputs names.

Example:

<input type="text" placeholder="number" name="fields[340]" required="" value="">
<input type="text" placeholder="number" name="fields[340]" required="" value="">

So now I need to synchronize inputs values.

Problem is that I don't know input names. This is what I have so far.

var $inputs = $(":input");
$inputs.keyup(function() {
     $inputs.val($(this).val());
});
like image 707
user2809327 Avatar asked Jan 25 '26 18:01

user2809327


1 Answers

Use 'input[name="' + this.name + '"]' selector to get the inputs with same name.

var $inputs = $(":input");
$inputs.on('input',function() {
  $('input[name="' + this.name + '"]').val($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<input type="text" placeholder="number" name="fields[340]" required="" value="">
<input type="text" placeholder="number" name="fields[340]" required="" value="">
<br/>
<input type="text" placeholder="number" name="fields[341]" required="" value="">
<input type="text" placeholder="number" name="fields[341]" required="" value="">
like image 139
rrk Avatar answered Jan 28 '26 10:01

rrk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!