-3

I’m having an issue with Chrome. I have tried to create a button that adds a new <input> tag when the button is pressed. Now, the below code works totally fine on Internet Explorer, however in Chrome nothing happens at all.

I also get no errors in the console; the function just doesn’t seem to run.

I’ve tried replacing the $("#addto").append(input1); with just alert("hello world");, just to see if the code ran at all, and it doesn’t.

I’ve also just noticed that this works fine if you put the JavaScript inside a <script> tag on the HTML page but it’s only when it’s in a separate .js file that it won’t work.

It looks like my Chrome version isn’t properly working. I’ve downloaded Chrome Portable and that has fixed the issue.

window.onload = function() {
  var buttonElement = document.getElementById("addinput");

  if (buttonElement) {
    buttonElement.addEventListener('click', addinput);
  }

  function addinput() {
    var input1 = '<div class="input-group mb-3"><div class="input-group-prepend"><span class="input-group-text" id="basic-addon1">Name</span></div><input type="text" class="form-control" placeholder="Username" aria-label="Username" aria-describedby="basic-addon1"></div>';
    
    $("#addto").append(input1);
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<div class="modal-footer">
  <button id="addinput" type="button" class="btn btn-default">add</button>
</div>
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
adam Wadsworth
  • 774
  • 1
  • 8
  • 26
  • 3
    This works fine for me once I added `jQuery` and `#addto`. To go off your edit, is the seperate file included in the html with a ` – Jimenemex Jul 25 '18 at 20:27
  • 1
    "but its only when it's in a separate js file that it wont work." Did you include that file? – Max Baldwin Jul 25 '18 at 20:31
  • @Jimenemex yes the file is in a script tag. and i know it's working as there are other function being call that draw a bar chart that shows. Also this all works in internet explorer. – adam Wadsworth Jul 25 '18 at 21:07

3 Answers3

0

window.onload=function() {
  var buttonElement = document.getElementById("addinput");

  if (buttonElement) {
    buttonElement.addEventListener('click', addinput);
  }

  function addinput() {
    var input1 = '<div class="input-group mb-3"><div class="input-group-prepend"><span class="input-group-text" id="basic-addon1">Name</span></div><input type="text" class="form-control" placeholder="Username" aria-label="Username" aria-describedby="basic-addon1"></div>';
    $("#addto").append(input1);
  }
}
 <div class="modal-footer">
        <button id="addinput" type="button" class="btn btn-default">add</button>
        <div id="addto"></div>
    </div><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>

Your code actually works

-1

If you are not using Jquery make sure you run the script after the button element is created.

If you're using jquery replace window.onload for

$(function(){
 //Initial code goes here
});

or

$( document).ready( function(){
 //Initial code goes here
});

the reason is because window.onload , runs the script on the go, probably before the button is even created, with jquery you must wait for the document to be fully loaded in order to work with elements

-1

One of solution put js file in before close tag </body> first jquery file, after yours, and you can remove window.onload

index.html

<head>

</head>

<body>
    <div class="modal-footer">
        <button id="addinput" type="button" class="btn btn-default">add</button>
        <div id="addto"></div>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
    <script src="./main.js">
    </script>
</body>

</html>

main.js

var buttonElement = document.getElementById("addinput");

if (buttonElement) {
    buttonElement.addEventListener('click', addinput);
}

function addinput() {
    var input1 = '<div class="input-group mb-3"><div class="input-group-prepend"><span class="input-group-text" id="basic-addon1">Name</span></div><input type="text" class="form-control" placeholder="Username" aria-label="Username" aria-describedby="basic-addon1"></div>';
    $("#addto").append(input1);
}