I'm trying to copy each username one by one from a div with the same class name into an input field.
I got it where it will copy all text from div to input on button click but how can I separate usernames?
copy names one by one to the input so if i press the button it copy name 1 to input and if i press button again it copy name 2 to input but removes name 1 and if press again it copy name 3 to input and removes name 2
how can i split so it just copy one user name to input?
var elems = document.getElementsByClassName("leaders");
var counter = elems.length;
function getUsers() {
var Elem;
if (counter <= 0)
counter = elems.length;
Elem = elems[counter - 1];
document.getElementById("inp").value = Elem.innerText.split(" — ")[0].split(", ");
counter--;
}
<div class="user-list">
<div class="leaders">user 1, user 2, user 3, user 4 — <strong>1</strong></div>
<div class="leaders">user 5, user 6, user 7, user 8 — <strong>2</strong></div>
<div class="leaders">user 9, user 10, user 11, user 12 — <strong>3</strong></div>
<div class="leaders">user 13, user 14, user 15, user 16 — <strong>4</strong></div>
<div class="leaders">user 17, user 18, user 19, user 20 — <strong>5</strong></div>
</div>
<br>
<input type="text" id="inp" style="width: 40%;">
<button style="width: 40%;" onclick="getUsers();">copy user name to input</button>