2009-12-23 18 views
5

Tengo un formulario html. Cuando hace clic en el botón, una función de JavaScript agrega un nuevo campo. Estoy intentando que la función también agregue una 'etiqueta' para el campo también.creando un campo de entrada para la forma html ... pero agregando una 'etiqueta' para eso también?

He intentado usar document.createElement ("Etiqueta"), pero eso no me deja cambiar el innerHtml (o tal vez estoy haciendo mal ..), ni añadir un cierre

Aquí es mi código ¡Gracias! var instance = 2;

 function newTextBox(element) 
     {  
      instance++; 
      // Create new input field 
      var newInput = document.createElement("INPUT"); 
      newInput.id = "text" + instance; 
      newInput.name = "text" + instance; 
      newInput.type = "text"; 
      instance++; 

      document.body.insertBefore(document.createElement("BR"), element); 
      document.body.insertBefore(newInput, element); 

     } 
    </script> 
</head> 


<body> 
    <LABEL for="text1">First name: </LABEL> 
    <input id="text1" type="text" name="text1"> 
    <LABEL for="text2">Last name: </LABEL> 
    <input id="text2" type="text" name="text2"> 



    <input type="button" id="btnAdd" value="New text box" onclick="newTextBox(this);" /> 
</body> 

+0

publicar su código donde ha intentado hacer un 'document.createElement ('etiqueta')' para poder depurarlo. – philfreo

+0

lo siento, lo borré porque pensé que era inútil: \t \t \t \t var label = document.createElement ("LABEL"); \t \t \t \t label.htmlPara = newInput.id; \t \t \t \t label.value = 'Nombre'; – Matt

+0

No existe tal cosa como 'label.value'. – bobince

Respuesta

6
function newTextBox(element) 
      {    
        instance++; 
        // Create new input field 
        var newInput = document.createElement("INPUT"); 
        newInput.id = "text" + instance; 
        newInput.name = "text" + instance; 
        newInput.type = "text"; 

        var label = document.createElement("Label"); 

        label.htmlFor = "text" + instance; 
        label.innerHTML="Hello"; 
        instance++; 

        document.body.insertBefore(document.createElement("BR"), element); 
        document.body.insertBefore(newInput,element); 
        document.body.insertBefore(label, newInput); 

Nota que for atributo de la etiqueta etiqueta, corresponde a la propiedad htmlFor fo el objeto etiqueta en javascript

+1

No puede usar 'label.for'. La respuesta de Proto Bassi es correcta. – Paul

+0

En realidad. Ambas respuestas están equivocadas de alguna manera. Ambos defienden el uso de innerHTML, que es malo pero más importante, el 'para' se establece usando label.htmlPara – Rafe

6

El ejemplo de Vincent no funciona.

Prueba esto:

var newlabel = document.createElement("Label"); 
    newlabel.setAttribute("for",id_from_input); 
    newlabel.innerHTML = "Here goes the text"; 
parentDiv.appendChild(newlabel); 
+2

, esta es la respuesta correcta. El atributo 'for' debe establecerse usando' setAttribute'. – Paul

+0

-1 para la sugerencia de creación dom y luego uso innerHTML en lugar de document.createTextNode – Rafe

+0

@Rafe, por favor, ejemplifique cómo establecerá el tipo a "Etiqueta" y cómo va a establecer un atributo en un TextNode. No creo que tu comentario sea de alguna manera útil. – Dinkheller

0
<div id="somediv"> 
some div 
</div> 

<script> 

var instance = 0; 

function newTextBox(element){    

instance++; 
// Create new input field 
var newInput = document.createElement("INPUT"); 
newInput.id = "text" + instance; 
newInput.name = "text" + instance; 
newInput.type = "text"; 

var newlabel = document.createElement("Label"); 
newlabel.setAttribute("for","text" + instance); 
newlabel.innerHTML = "Here goes the text"; 
document.getElementById("somediv").appendChild(newlabel); 
document.getElementById("somediv").appendChild(newInput); 
} 

</script> 
Cuestiones relacionadas