Home > Net >  How to apply regex to multiple inputs at the same time
How to apply regex to multiple inputs at the same time

Time:06-05

I' new to programming, trying to apply a regex that replaces every space (' ') with a dash ('-') but the querySelectorAll() returns a NodeList, after some struggle trying to get an Array from so i could use forEach(), now i step in this error.

If possible, i do intend to use a few other regular expressions to validate if there's http/https in some fields so if the solution does allow me to apply other regex later would be awesome

const reset = document.querySelector(".reset-button");
const submit = document.querySelector("#generate-utm");
const form = document.querySelector(".data-form");
const inputs = document.querySelectorAll(".data-input");
const copyButton = document.querySelector(".copy-button");


Array.from(inputs).forEach(() => {
  addEventListener("keyup", (e) => {
    e.value = e.value.replace(/ /g, "-");
  });
});

//end inputs logic

// buttons logic

submit.addEventListener("click", (e) => {
  e.preventDefault();
  const url = document.querySelector(".url").value;
  const source = document.querySelector(".source").value;
  const type = document.querySelector(".type").value;
  const name = document.querySelector(".name").value;
  const term = document.querySelector(".term").value;
  const content = document.querySelector(".content").value;
  const output = document.querySelector(".data-output");

  output.value = `${url}?utm_source=${source}&utm_medium=${type}&utm_campaign=${name}&utm_term=${term}&utm_content=${content}`;
});

reset.addEventListener("click", (e) => {
  e.preventDefault();
  reset.classList.toggle("rotate");
  form.reset();
});

copyButton.addEventListener("click", (e) => {
  e.preventDefault();
  const dataOutput = document.querySelector(".data-output");

  if (!navigator.clipboard) {
    dataOutput.select();
    document.execCommand("copy");
  } else {
    navigator.clipboard
      .writeText(dataOutput.value)
      .then(function () {
        alert("abrir popup!");
      })
      .catch(function () {
        alert("Erro ao copiar, tente copiar manualmente");
      });
  }
});

// end buttons logic
<header>
        <h1 >Gerador de UTM<span style="color: hsl(360, 69%, 42%);">.</span></h1>
    </header>
    <main>
        <div >
            <form >
                <div >
                    <div >
                        <input type="text"  placeholder="URL do site">
                        <input type="text"  placeholder="Fonte da campanha">
                        <input type="text"  placeholder="Tipo da campanha">
                    </div>
                    <div >
                        <input type="text"  placeholder="Nome da campanha">
                        <input type="text"  placeholder="Termos da campanha">
                        <input type="text"  placeholder="Conteúdo da campanha">
                    </div>
                </div>
                <div >
                    <input type="submit"  id="generate-utm" value="Gerar UTM">
                    <button >
                            refresh
                        </button>
                </div>
            </form>
        </div>
        <div >
            <textarea id="" cols="30" rows="10"  placeholder="Sua UTM aparecerá aqui"
                disabled></textarea>
            <input type="submit"  value="Copiar">
        </div>
    </main>

CodePudding user response:

Just use e.target to access the actual HTML element.

Here's the updated code:

Array.from(inputs).forEach(() => {
  addEventListener("keyup", (e) => {
  //console.log("evaluacion",e.target)
    e.target.value = e.target.value.replace(/ /g, "-");
  });
});

//end inputs logic

// buttons logic
  • Related