Copy HTML elements into a form

Asked

Viewed 50 times

0

I want to copy the div clicked, into the form which already contains the select.

For example this:

        var opt = document;

        function processar(el) {
            opt.getElementById('minhaLista').innerHTML = el;
        }
    <form name="form" id="minhaLista">
        <select>
          <option value="">A</option>
          <option value="">B</option>
          <option value="">C</option>
        </select>
    </form>

    <hr size="1">

    <div id="vitrine">
        <div id="produto1">
            <a href="javascript:void(0)" onclick="processar(produto1)">
<img src="https://sites.google.com/site/mplayerplugin/repositorio/procurando_dory.jpg" width="300" />
</a>
            <div>
                A
                <p>Exemplo 1</p>
                <span>1</span>
            </div>
        </div>

        <hr size="1">

        <div id="produto2">
            <a href="javascript:void(0)" onclick="processar(produto2)">
<img src="https://sites.google.com/site/mplayerplugin/repositorio/big_buck_bunny.jpg" width="300" />
</a>
            <div>
                B
                <p>Exemplo 2</p>
                <span>2</span>
            </div>
        </div>
        <hr size="1">

            <div id="produto3">
                <a href="javascript:void(0)" onclick="processar(produto3)">
                <img src="https://sites.google.com/site/mplayerplugin/repositorio/animais_cantando.jpg" width="300" />
</a>
                <div>
                    C
                    <p>Exemplo 3</p>
                    <span>3</span>
                </div>
            </div>
    </div>

The main focus is the image element [img], paragraph [p] and text [span].

The select must remain within the form, without removal by script.

Perhaps the correct term is to perform a replica of div to the form.

However simple it may seem, unfortunately I could not.

  • 1

    Use the property outerHTML, for ex: opt.getElementById('minhaLista').innerHTML = el.outerHTML;

  • 1

    The el is an object, so you can’t add it with innerHTML (unless you use outerHTML or innerHTML on the object to be replicated). select, you can add a div within the form and, instead of using opt.getElementById('minhaLista'), you use opt.getElementById('minhaLista_minha_div').innerHTML = el.outerHTML;

  • 1

    https://developer.mozilla.org/en-US/docs/Web/API/Element/outerHTML

  • @Valdeirpsr Grateful for the good will and explanation as well as the link that points me.

  • 1

    Check this: https://codepen.io/valdeir2000/pen/MXaEpo

No answers

Browser other questions tagged

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