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

208
Visualizações
How to Make This Drag n Droppable

I'm trying to make a form element drag and droppable, but I can't make it work. Any ideas? I'm trying to make it be able to be dropped into another element.

Here is the code for the form element:

<div class="text1">
    <form>
        Item Name:<br>
        <input type="text" name="fname">
    </form>
</div>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The code below is based on the HTML5 Drag and Drop API. When you drop it into a box, it will execute a function that will drop it there.

function allowDrop(ev) {
  ev.preventDefault();
}

function drag(ev) {
  ev.dataTransfer.setData("text", ev.target.id);
}

function drop(ev) {
  ev.preventDefault();
  var data = ev.dataTransfer.getData("text");
  ev.target.appendChild(document.getElementById(data));
}
#div1 {
  width: 350px;
  height: 70px;
  padding: 10px;
  /* not required, just shows hitboxes */
  border: 2px solid lime;
}

#div2 {
  width: 350px;
  height: 70px;
  padding: 10px;
  /* not required, just shows hitboxes */
  border: 2px solid magenta;
}

#drag1 {
  /* not required, just shows hitboxes */
  display: inline-block;
  border: 2px solid cyan;
}
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<br>
<div class="text1" id="drag1" draggable="true" ondragstart="drag(event)">
    <form>
        Item Name:<br>
        <input type="text" name="fname">
    </form>
</div>
<br>
<div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

I added IDs because they are required for it to work.

This code adds two boxes that you can drag and drop an element into. It can also be dropped between pages.

Since a form input is also classified as something that can have stuff dropped into, I wouldn't recommend drag and dropping a form with an input. You will get a hierarchy error if someone miss-clicks, and the DnD will refuse to work until you reload.

More information is on these pages: MDN webdev

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