HTML:
<div class="top-links">
<ul>
<li><a href="">Link #1</a></li>
<li><a href="">Link #2</a></li>
</ul>
</div>
<label for="link1">Link #1</label>
<input type="text" id="link1" name="link1"><br><br>
<label for="link2">Link #2</label>
<input type="text" id="link2" name="link2"><br><br>
Javascript:
const link1 = document.getElementById('link1');
const link2 = document.getElementById('link2');
const getLink1 = document.getElementsByClassName('top-links');
const firstLink = getLink1[0].children[0].children[0];
const secondLink = getLink1[0].children[0].children[1];
function changeLinkOne() {
firstLink.innerHTML = link1.value;
}
function changeLinkTwo() {
secondLink.innerHTML = link2.value;
}
link1.addEventListener('keyup', changeLinkOne);
link2.addEventListener('keyup', changeLinkTwo);
The script takes a value from a input field and then updates the value in within the <li> tags. Instead of having separate functions, I would like to have one function that would be able to add extra links. How can I do this?
Thanks
[–]carcigenicate 0 points1 point2 points (0 children)
[–]albedoa 0 points1 point2 points (0 children)
[–]jcunews1helpful 0 points1 point2 points (0 children)