'Changing onClick attribute with ajax does not fire the new event?
Just tried out the following approach finding that this does not work?
(Using jQuery):
function bookmark_add() {
$.ajax({
type: "POST",
url: "load.php",
data: data,
success: function(msg) {
var msg_array=msg.split("-");
var success=msg_array[0];
var bookmark_id=msg_array[1];
if(success==1) {
$('.btn_bookmark').html('Remove Bookmark');
$('.a_bookmark').attr("onClick","bookmark_remove("+bookmark_id+"), return false;");
}
}
});
}
This function works fine. The attribute of the <a> element is being changed correctly. However, the new event (the function bookmark_remove is not being fired. So I assume that my approach does not work because of some basic misunderstanding, probably?
Could anyone tell me that this assumption is right and give any hint why?
Solution 1:[1]
"bookmark_remove("+bookmark_id+"), return false;"
The comma is wrong. it should be a semicolon.
Solution 2:[2]
Since you are using jQuery, instead of:
$('.a_bookmark').attr("onClick","bookmark_remove("+bookmark_id+"), return false;");
I suggest you use this form:
$('.a_bookmark').click(function(){
bookmark_remove(bookmark_id);
return false;
})
more readable with less quotes tokens, right?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Wampie Driessen |
| Solution 2 | Derek 朕會功夫 |
