Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading