Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

175
Visualizações
Why isn't my element being added to the page?

I want to add a input element on the DOM tree with an attribute of type=text with jQuery. However it seems to not work. The DOM tree is the same and no console errors.

$(document).ready(function() {
  var $code = $("#command");
  var $text = $("#itemtext");
  var $count = $('#count');
  var $add = $('add');

  $add.click(function() {
    $("#items").last().after('<input type="text" id="itemtext" />');
  });
  $("#generatebutton").click(function() {
    $code.text('/give @p bundle{Items:[{id:' + $text.val() + ',Count:' + $count.val() + '}]}');

  });
});
<body>
  <h1 id="title">Bundle Generator for mc</h1>
  <div id="items">
    <input type="text" placeholder="item name" id="itemtext">
    <input type="number" min="-128" max="127" id="count">
    <button id="addslot">Add slot</button>
    <button id="generatebutton">Generate!</button>
  </div>
  <h2>Your command is here!</h2>
  <p id="command"></p>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</body>

this line:

$("#items").last().after('<input type="text" id="itemtext" />');

doesn't work, can anyone help me?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You have mentioned an id that isn't in DOM. Maybe you are looking for addslot instead of add? Also there is no indication of id ('#') in var $add = $('add').

$(document).ready(function() {
  var $code = $("#command");
  var $text = $("#itemtext");
  var $count = $('#count');
  var $add = $('#addslot');

  $add.click(function() {
    $("#items").last().after('<input type="text" id="itemtext" />');
  });
  $("#generatebutton").click(function() {
    $code.text('/give @p bundle{Items:[{id:' + $text.val() + ',Count:' + $count.val() + '}]}');

  });
});
<body>
  <h1 id="title">Bundle Generator for mc</h1>
  <div id="items">
    <input type="text" placeholder="item name" id="itemtext">
    <input type="number" min="-128" max="127" id="count">
    <button id="addslot">Add slot</button>
    <button id="generatebutton">Generate!</button>
  </div>
  <h2>Your command is here!</h2>
  <p id="command"></p>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</body>

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda