nameServerInput.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Attaches listeners to nameserver add and remove links to clone or remove the input
  2. export default function handleNameServerInput() {
  3. // Add new name server input
  4. const addNsButton = document.querySelector('.js-add-ns');
  5. if (addNsButton) {
  6. addNsButton.addEventListener('click', () => addNsInput(addNsButton));
  7. }
  8. // Remove name server input
  9. document.querySelectorAll('.js-remove-ns').forEach((removeNsElem) => {
  10. removeNsElem.addEventListener('click', () => removeNsInput(removeNsElem));
  11. });
  12. }
  13. function addNsInput(addNsButton) {
  14. const currentNsInputs = document.querySelectorAll('input[name^=v_ns]');
  15. const inputCount = currentNsInputs.length;
  16. if (inputCount < 8) {
  17. const template = currentNsInputs[0].parentElement.cloneNode(true);
  18. const templateNsInput = template.querySelector('input');
  19. templateNsInput.removeAttribute('value');
  20. templateNsInput.name = `v_ns${inputCount + 1}`;
  21. addNsButton.before(template);
  22. }
  23. if (inputCount === 7) {
  24. addNsButton.classList.add('u-hidden');
  25. }
  26. }
  27. function removeNsInput(removeNsElement) {
  28. removeNsElement.parentElement.remove();
  29. const currentNsInputs = document.querySelectorAll('input[name^=v_ns]');
  30. currentNsInputs.forEach((input, index) => (input.name = `v_ns${index + 1}`));
  31. document.querySelector('.js-add-ns').classList.remove('u-hidden');
  32. }