TEsting'; ...">

Button Click from PHP not responding

I am repeating a button from a php script like this: print'<button id="testingbtn">TEsting</button>';

I am using id to listen to the button and alert the notification message like this:

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

      

The thing is, I used the same method before and it worked for me in all browsers, but not anymore. can someone try to help me solve this problem. Thanks guys..

+3


source to share


4 answers


Try this, we assume your "testbtn" is dynamically added to the screen.



$(document).ready(function(){
    $( "body" ).on( "click", "#testingbtn", function() {
                    alert('working');
    });
});

      

+3


source


I am not using printing. I always use echo to show it in my html page.

<?php
 echo '<button id="testingbtn">TEsting</button>';
?> 

      

In jquery:



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

      

Try reading this

+4


source


I never recommend doing it this way. Always put this code outside of php.

Html

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

      

JQuery

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

      

Also make sure jQuery is included in your code.

+3


source


Point 1: Check if jquery library is included or not.

Point 2: [If point 1 is ok] where did you put your script? If you put your script before button code than using document ready function

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

      

Alternatively if you want to put your code unchanged than put your script after the button code [best practice: put them at the bottom of the page].

+3


source







All Articles