Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

155
Visualizações
Use one modal for all entries javascript

I'm trying to send the id of the object that we select to edit in the form action.

 let edit_buttons = document.querySelectorAll(".edit-modal");

  for(var i = 0; i < edit_buttons.length; i++) {
    edit_buttons[i].addEventListener("click", function(e) {
    console.log(`${this.getAttribute("data-id")}`)
    console.log(`{% url 'users:edit' pk=1 %}`)
    document.querySelector("form").setAttribute("action", `{% url 'internal-users:edit-product' pk=${this.getAttribute("data-id")} %}`);
    })
  }
  <tr>
    <td class="border-0 size-13 text-gray-dark">
      <button class="btn btn-gray text-gray size-13 edit-modal"  data-id="1" data-toggle="modal" data-target="#editModal">Edit</button>
    </td>
  </tr>

  <tr>
    <td class="border-0 size-13 text-gray-dark">
      <button class="btn btn-gray text-gray size-13 edit-modal"  data-id="2" data-toggle="modal" data-target="#editModal">Edit</button>
    </td>
  </tr>
{% endfor %}


<form method="POST" action="">
  {% csrf_token %}
  <input type="hidden" id="object_id">
  
</form>

So if we choose first entrie to edit I want the action of the form to be /users/1/edit/

If I try to add with javascript I get the following error

Could not parse the remainder: '${this.getAttribute("data-id")}' from '${this.getAttribute("data-id")}'

If someone can help me I would be very happy, thanks in advance!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Delegate

document.querySelector("#myTable tbody").addEventListener("click", function(e) {
  const tgt = e.target;
  if (tgt.classList.contains("edit-modal")) {
    document.getElementById("postForm").action = `someurl?id=${tgt.dataset.id}`;
    console.log(document.getElementById("postForm").action)
  }
})
<table id="myTable">
  <tbody>
    <tr>
      <td class="border-0 size-13 text-gray-dark">
        <button class="btn btn-gray text-gray size-13 edit-modal" data-id="1" data-toggle="modal" data-target="#editModal">Edit</button>
      </td>
    </tr>

    <tr>
      <td class="border-0 size-13 text-gray-dark">
        <button class="btn btn-gray text-gray size-13 edit-modal" data-id="2" data-toggle="modal" data-target="#editModal">Edit</button>
      </td>
    </tr>
  </tbody>
</table>


<form method="POST" id="postForm" action="">

</form>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda