JSFiddle - React, Tailwind, and code Playground
HTML
<input id="a" type="checkbox" />
<input type="text" id="foo[1]" name="foo[1]" />
<input type="text" id="foo[2]" name="foo[2]" />
<input type="text" id="foo[3]" name="foo[3]" />
<input type="text" id="foo[4]" name="foo[4]" />
<input type="text" id="foo[5]" name="foo[5]" />
JavaScript
$("#a").click(function() {
var first = $('[id=foo[1]]');
var next = first.nextAll(':text').filter(function() {
console.log(this.id);
return /foo\[\d+\]/.test(this.id);
});
if($(this).is(':checked')) {
next.val(first.val());
}
else {
next.val('');
}
});