I'm trying to create a add/remove favorite with jquery and php. The addfavorite function works but when i change the id attribute of the link from addfavorite to removefavorite it doesn't work. The id changed but it still doing addfavorite function. If i open firebug i see that the ID changed in id="removefavorite", but when i open the page source it didn't change ID='addfavorite'
Ho can i change with jquery the ID of a link even in the dom?
JQUERY:
$(document).ready(function() {
$('#addfavorite').click(function() {
id = $('#item').attr('value');
$.ajax({
type: "POST",
url: "http://127.0.1.1/zend/fm/public/video/addfavorite",
data: "id_video="+id,
cache: false,
async: false,
success: function(result) {
$('#addfavorite').attr('id','removefavorite');
$('#removefavorite').text('Remove from favorite');
getRating(text);
},
error: function(result) {
alert("some error occured, please try again later");
}
});
});
$('#removefavorite').click(function() {
id = $('#item').attr('value');
$.ajax({
type: "POST",
url: "http://127.0.1.1/zend/fm/public/video/removefavorite",
data: "id_video="+id,
cache: false,
async: false,
success: function(result) {
$('#removefavorite').attr('id','removefavorite');
$('#removefavorite').text('Remove from favorite');
getRating(text);
},
error: function(result) {
alert("some error occured, please try again later");
}
});
});
)};
HTML:
<a href="javascript:void(0)" id="addfavorite">
Add to favorite
</a>
Don't change
id
of an element just change the label. I think it will help you.