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>