In my php page i append new buttons and information when press the button with JQuery Ajax,however when i click the new button which came with ajax , its JQuery does not work but old button's JQuery works,i mean new button does not see the javascript file of my php page.
My JQuery Ajax Codes;
$("button[name='addnewelement']").click(function() {
$.ajax({
type: "POST",
url: "addelement.php",
cache: false,
success: function(html){
$("#new").append(html);// add new element into index.php
});
});
addelement.php;
echo "
.......
<button id="<?php echo $row['id']; ?>" name="addnewelement" >Add</button>
.......
" ;
How can i solve this problem?
Thanks.
Your echo
in the addelement.php
page would be like
echo '<button id="'.$row['id'].'" name="addnewelement" >Add</button>';
exit;
And also better to put the exit
after the echo
.Make sure that you follow the syntax and use a good IDE to find the syntax errors.
You should use on click event handler instead of click event handler.
Here is the link : http://api.jquery.com/on/
eg.
$( document).on( "click", "button[name='addnewelement']", function() {
$.ajax({
type: "POST",
url: "addelement.php",
cache: false,
success: function(html){
$("#new").append(html);// add new element into index.php
});
});