moji
moji

Reputation: 267

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..

Upvotes: 3

Views: 332

Answers (4)

aldrin27
aldrin27

Reputation: 3407

I don't use print. 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

Upvotes: 2

Al Amin Chayan
Al Amin Chayan

Reputation: 2500

Point 1: Check whether you have included the jquery library or not.

Point 2: [If point 1 is OK], Where have you put your script? If you put your script before button code, than use document ready function

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

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

Upvotes: 3

Domain
Domain

Reputation: 11808

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

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

Upvotes: 3

Akshay
Akshay

Reputation: 2229

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.

Upvotes: 3

Related Questions