Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Detect Change of Input on Page Load by PHP

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?

like image 414
Bijan Avatar asked Jan 29 '26 23:01

Bijan


2 Answers

Trigger input change on load:

$("#URL").trigger('input');
like image 171
Karlo Kokkak Avatar answered Jan 31 '26 13:01

Karlo Kokkak


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/

like image 22
Anderson Contreira Avatar answered Jan 31 '26 13:01

Anderson Contreira



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!