If you are just wanting to take the current input value and copy it to the first other input you find that begins with the same name... (Not sure this is what you are after, but...)
<!-- using onClick(),or use addListener() if you prefer -->
<input type="text" name="myName" value="abd" id=srcInput onClick="copyThis2That(this)"/>
<input type="text" name="myName1" value="" />
<input type="text" name="myName2" value="" />
<input type="text" name="myName3" value="" />
You can move your call to copyThis2That()
to a button, or anything else you like, so long as you reference it back to the specific input whose value you want to copy by sending copyThis2That(document.getElementById("srcInput"))
as the parameter to the function.
Then...
<script>
function copyThis2That(e) {
var x = document.querySelectorAll("input");
for (var i = 0; i < x.length; i++) {
if (x[i].name) {
if (x[i].name !== e.name && (x[i].name + " ".repeat(100)).substr(0,e.name.length) == e.name) {
x[i].value = e.value;
}
}
}
}
</script>
description: for each input on page, 1) IF it HAS a name, and 2) if that name is not exactly equal to the source input's name (ie, begins with it but is longer), and 3) if it BEGINS with the same as the source input's entire name, THEN copy the value to this input.
- IF it HAS a name, and
- if that name is not exactly equal to the source input's name (ie, begins with it but is longer), and
- if it BEGINS with the same as the source input's entire name, THEN copy the value to this input.
Again, this copies the source input value to the FIRST other input it finds that begins with the same characters as the source input name.
This snippet assumes that you have no elements on the page whose name is longer than 100 characters.