How to join texts in a textbox by dragging them?

Asked

Viewed 37 times

2

I have an empty textbox, and I have some buttons with values like:

+, -, *, /, palavra1, palavra2

so on and so forth.

I want to fill this textbox by dragging these buttons into it, not just by clicking on them, I saw something that can be done in HTML5/Javascript, but I couldn’t solve it.

Note: I would click and hold the mouse and drag this button inside the textbox and the textbox would pick up the value of this button, as if it were a true lego by mounting inside the textbox the strings.

Note 2: It has to be with buttons or inputs, not with images.

1 answer

2


This can be solved using html and javascript.

Your html would look like this:

  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.innerHTML+=" <p>"+document.getElementById(data).innerHTML+"</p>";
            
        }
.div 
        {
            width:350px;height:70px;padding:10px;border:1px solid #aaaaaa;
        }
<div class="div" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div class="div" ondrop="drop(event)" ondragover="allowDrop(event)">
    <button id="drag1" draggable="true" ondragstart="drag(event)" width="336" height="69">Code</button>
    <button id="drag2" draggable="true" ondragstart="drag(event)" width="336" height="69">Code</button>
</div>

Can run to test, I think this solution perfectly suits you.

  • Thanks for the answer, I circled here, but for some reason, I try to drag the button to this div, and it does not take the value and does not accomplish anything, I tested in my project and also did not run.

  • puts your entire code that ae we try to run, like it on online javascript sites, eg: jsfiddle.net

  • https://jsfiddle.net/9kx6zvva/

  • I put in the simplest way possible what I’m looking for, I put textarea but it can be up to a div, I just don’t send all the code by the size of it ..

  • This file sent me this without the js

  • https://jsfiddle.net/zft0tpos/ updated

  • You are using normal inputs and not buttons, try to take the example and change.

  • 1

    I used your own example, and it didn’t work here, but I found other similar examples and they didn’t run very well, I believe the problem is in my machine, but your answer is correct in a certain way.

  • 1

    found the problem, it was my mouse that did not select kk, thank you very much for the patience, theme solved.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.