user2809327
user2809327

Reputation: 157

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());
});

Upvotes: 3

Views: 1110

Answers (1)

rrk
rrk

Reputation: 15846

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="">

Upvotes: 6

Related Questions