I load a PHP code into a div
:
$(".loader").click(function(e) {
event.preventDefault();
$("#content").load($(this).attr("href"));
});
in the target code there's some html5 stuff, i.e.:
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="inputFwFile">Upload</span>
</div>
<div class="custom-file">
<input type="file" class="custom-file-input" id="inputFwFile" aria-describedby="inputFwFile">
<label class="custom-file-label" for="inputFwFile">Choose file</label>
</div>
</div>
and in my js file I want to catch some events:
$("#inputFwFile").change(function(e) {
console.log(e.target.files);
});
Please note all the jQuery functions are wrapped into $("document").ready(function() ...
statement.
The code above works if the html snippet is placed into the main page (index.php). Instead, when loaded into its div content
doesn't execute the change
event anymore.
Perhaps it's because when the dom is ready the specific #intputFwFile
doesn't exist yet?
Any way to fix?
UPDATE
After reading the comment I tried this:
index.php file
<div id="content">
</div>
index.js
$("document").ready(function() {
$(".loader").click(function(e) {
event.preventDefault();
$("#content").load($(this).attr("href"));
});
});
file.php
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="inputFwFile">Upload</span>
</div>
<div class="custom-file">
<input type="file" class="custom-file-input" id="inputFwFile" aria-describedby="inputFwFile">
<label class="custom-file-label" for="inputFwFile">Choose file</label>
</div>
</div>
<script type="text/javascript">
$("document").ready(function() {
$("#inputFwFile").change(function(e) {
console.log(e.target.files);
});
});
</script>
but still the change
function is not executed even if it's in the same file of the requested element and the script runs when the document has loaded.
Why?