Reputation: 1
I am trying to refactor some of my code. Essentially I am using an button onClick event to invoke a function that copies a hidden input value by its element id. Since I have so many buttons(nearly 30) I am trying to condense all of these functions down to 1 or 2 instead of having a unique function for each unique ID. Any suggestions?
HTML
<input class="input" type="hidden" value="Test" id="accept1" size="1" on style="opacity:0">
<button onclick= "acceptFunction1()" > Test </button>
JavaScript
function acceptFunction1() {
var copyText = document.getElementById("accept1");
copyText.type = 'text';
copyText.select();
copyText.setSelectionRange(0, 99999);
document.execCommand("copy");
copyText.type = 'hidden';
}
Upvotes: 0
Views: 105
Reputation: 11
You can pass the name of the field(s) that you want to operate on. An example might be:
<input class="input" type="hidden" value="Test" id="accept1" size="1" on
style="opacity:0">
<button onclick= "acceptFunction('accept1')" > Test </button>
function acceptFunction(fldName) {
var copyText = document.getElementById(fldName);
copyText.type = 'text';
copyText.select();
copyText.setSelectionRange(0, 99999);
document.execCommand("copy");
copyText.type = 'hidden';
}
Upvotes: 1