Add a new label to every button click

Asked

Viewed 342 times

4

This is probably a very simple question, but I’m new to programming and I need a little help. I need every time I click a button a new label appears, but you need to add that label in a way that I can add as many Abels as necessary, but I don’t even know how to get it right. Follows my code:

Edit: I expressed myself badly, what I really wanted was the input, I managed to resolve thanks for the help

var botao = document.getElementById("btn");

botao.addEventListener('click', function() {

})
Valor do produto:<input type="text" name="valor1" />
<br />
valor 2: <input type="text" name="valor2" />
<br />
valor final: <input type="text" name="valor final" />
<br/>
<button id="btn">Adicionar novo valor</button>

  • And If the guy clicks 10x gets 10 equal Abels? What will be on this label?

  • are basically are the same and with label I refer to inputs, but the idea is that they have different values, as they will be added at the end, because of this they need to have different names or ids, but this I arrange later

3 answers

3


You can use the innerHTML through javascript to do this. I created a div just to say where I want to create one more element, so I take this element with querySelector by javascript and use the innerHTML click to add the new element.

        var botao = document.getElementById("btn");
        var body = document.querySelector('.box');
          botao.addEventListener('click', function () {
          body.innerHTML += "<label>nova label:</label></br>"
         
        })
<div class="box">
Valor do produto:<input type="text" name="valor1" /><br />
    valor 2:⠀⠀⠀⠀⠀⠀<input type="text" name="valor2" /><br />
    valor final:⠀⠀⠀⠀<input type="text" name="valor final" /> <br/>
  </div>
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀<button id="btn">Adicionar novo valor</button>

  • I just wanted to make a correction because I ended up expressing myself badly what I really wanted was the input, so I changed body.innerHTML += "<label>new label:</label></br>" to "New value: <input type="text" name="new value" /> <br/>"

0

The most appropriate would be to use the method createElement (dynamically create an element). This is because using innerHTML, the values typed in all fields will be deleted. Now, to make the procedure easier, create a span with a id where the new fields will be inserted:

<span id="valores">
   valor 2: <input type="text" name="valor2" />
</span>

The span is useful in such cases because it serves as a reference container and does not affect the layout.

See how it would look (explanatory comments in the code):

var botao = document.getElementById("btn");

botao.addEventListener('click', function() {

   // pega a span pelo id
   var vals = document.getElementById("valores");

   // cria a tag <br>
   var br = document.createElement("br");
   // insere no span
   vals.appendChild(br);

   // pega todos os inputs dentro do span
   var qtd = document.querySelectorAll("#valores input");
   // como já tem um, soma com 2 para criar uma contagem sequencial
   var names = qtd.length + 2;

   // cria o nó de texto
   var lab = document.createTextNode("Valor "+ names +": ");   
   // insere no span
   vals.appendChild(lab);

   // cria o input
   var inp = document.createElement("input");
   // cria atributo "type"
   inp.type = "text";
   // cria atributo "name" com o valor sequencial
   inp.name = "valor"+names;
   // insere no span
   vals.appendChild(inp);
   
   
});
Valor do produto:<input type="text" name="valor1" />
<br />
<span id="valores">
   valor 2: <input type="text" name="valor2" value="10,20" />
</span>
<br />
valor final: <input type="text" name="valor final" />
<br/>
<button id="btn">Adicionar novo valor</button>

-3

Let me see if I understand, you want to click on the button "add new value", be appendado a label with input so that the user can enter one more record? If yes, use one:

$( ".inner" ).append( "<p>Test</p>" );

Where the .inner you substitute for the last record, it may be the div who is with the label and input inside. And where is the append you put the input that you want. If that’s what you say I’ll help you build the code.

  • Hello @Analyze welcome to Stackoverflow ;) if you look at the bar on top of the text editor when you reply, see that there is a button with this symbol <> (or the shortcut Ctrl+M), in it you can add codes html, css and javascript, so it already comes properly formatted for better reading of users and can still be run to see the result.

  • 1

    Also note that it is using pure javascript, without Jquery.

Browser other questions tagged

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