JavaScript - createElement

 
Vista:
sin imagen de perfil
Val: 17
Ha disminuido su posición en 3 puestos en JavaScript (en relación al último mes)
Gráfica de JavaScript

createElement

Publicado por Diego Andres (12 intervenciones) el 18/04/2020 22:51:35
Buen Día.

Tengo este código(que mostrare a continuación), funciona pero quisiera colocar que los datos no se repitieran al darle click, que solo se creara un solo elemento ya que si le doy muchas veces se crean el mismo elemento
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <input type="text" id="texto">
        <button type="button" id="boton" onclick="agregar()">Registrar</button>
    </body>
    <script>
        function agregar() {
            var crear = document.createElement("FORM");
 
            crear.setAttribute("id", "Formulario");
            document.body.appendChild(crear);
 
            var colocar = document.getElementById("texto").value;
            var genera = document.createElement("INPUT");
            genera.setAttribute("type", "text");
            genera.setAttribute("value", colocar);
            genera.setAttribute("class", "inputcreado");
            document.getElementById("Formulario").appendChild(genera);
        }
    </script>
</html>

Les agradeceria sus respuestas buen dia.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de joel
Val: 3.506
Oro
Ha mantenido su posición en JavaScript (en relación al último mes)
Gráfica de JavaScript

createElement

Publicado por joel (895 intervenciones) el 19/04/2020 11:18:25
Hola Diego, segun entiendo, solo quieres que se cree un solo formulario nuevo, verdad... la manera que yo haria seria comprobando si ya existe dicho diccionario... algo así:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <input type="text" id="texto">
        <button type="button" id="boton" onclick="agregar()">Registrar</button>
    </body>
    <script>
        function agregar() {
            if(document.querySelector("#Formulario")) {
                return;
            }
            var crear = document.createElement("FORM");
 
            crear.setAttribute("id", "Formulario");
            document.body.appendChild(crear);
 
            var colocar = document.getElementById("texto").value;
            var genera = document.createElement("INPUT");
            genera.setAttribute("type", "text");
            genera.setAttribute("value", colocar);
            genera.setAttribute("class", "inputcreado");
            document.getElementById("Formulario").appendChild(genera);
        }
    </script>
</html>

Es esto lo que quieres?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar