In my page I have a jQuery snippet:
$( document ).ready(function() {
$("#URL").on("input load", function(e) {
console.log(e.type)
....
});
});
lower on the page I have..
<input id="URL" type="text" value="<?=$_GET['URL'];?>">
to set the value to the $_GET['URL'] value if it is set.
For some reason, the jQuery is never triggered unless I go and change the input.
Is there a way to have the jQuery trigger when I set the value via PHP?
Trigger input change on load:
$("#URL").trigger('input');
I believe that you need trigger an action like input, for example:
$( document ).ready(function() {
var fn = function(e) {
console.log(e.type)
//...
$("body").append("<p>"+e.type+"</p>")
};
$("#URL").on("input", fn);
$("#URL").on("load", fn);
//Force the execution on load
$("#URL").trigger('input');
});
Fiddle: https://jsfiddle.net/andersoncontreira/u8jLpj1g/3/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With