Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button Click from PHP is not responding

I echo a button from a php script as following: print'<button id="testingbtn">TEsting</button>';

I use the id listen to the button and alert a notification message as following:

            $('#testingbtn').click(function(){
                alert('working');
            });

The thing is that i used the same method before and it worked for me on all browsers but not anymore. can someone try to help me out the problem. thanks guys..

like image 237
moji Avatar asked Feb 19 '26 17:02

moji


2 Answers

Try this, we assuming your 'testingbtn' is dynamically added to the screen.

$(document).ready(function(){
    $( "body" ).on( "click", "#testingbtn", function() {
                    alert('working');
    });
});
like image 156
Domain Avatar answered Feb 21 '26 06:02

Domain


I never recommend to do it in this way. Always put this code outside php.

HTML

<button id="testingbtn">TEsting</button>;

jQuery

 $('#testingbtn').click(function(){
                alert('working');
            });

Also make sure jQuery is included in your code.

like image 26
Akshay Avatar answered Feb 21 '26 07:02

Akshay



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!