I am currently working on a project, where I want to make a click on an object change a class and then afterwards making the next click depend on the same jquery.
I can only get it to work the first time - afterwards it won't work. My source is as follows:
$(document).ready(function(){
$("a.activate").click(function(){
var elmid = $(this).attr('id');
$.ajax({
type: "POST",
dataType: "json",
url: "/advertisement/activate", // URL of the Perl script
data: {adID: $(this).prev("input.ad_ID").val()},
success: function(data){
if (data.error) {
$('div#create_createresult').text(data.msg);
$('div#create_createresult').addClass("text-danger");
$("form#createForm input#createForm_submit").removeAttr('disabled');
}
else
{
var src = $("a#" + elmid + " img").attr("src").replace("activate-16x16.png", "deactivate-16x16.png");
$("a#" + elmid + " img").attr("src", src);
$("a#" + elmid).removeClass('activate').addClass('deactivate');
$('div#create_createresult').text(data.msg);
$('div#create_createresult').addClass("success");
}
}
});
});
$("a.deactivate").click(function(){
var elmid = $(this).attr('id');
$.ajax({
type: "POST",
dataType: "json",
url: "/advertisement/deactivate", // URL of the Perl script
data: {adID: $(this).prev("input.ad_ID").val()},
success: function(data){
if (data.error) {
$('div#create_createresult').text(data.msg);
$('div#create_createresult').addClass("text-danger");
$("form#createForm input#createForm_submit").removeAttr('disabled');
}
else
{
var src = $("a#" + elmid + " img").attr("src").replace("deactivate-16x16.png", "activate-16x16.png");
$("a#" + elmid + " img").attr("src", src);
$("a#" + elmid).removeClass('deactivate').addClass('activate');
$('div#create_createresult').text(data.msg);
$('div#create_createresult').addClass("success");
}
}
});
});
});
My new source (as of Barbara's comment) is:
$(document).ready(function(){
$("a.activate").on( 'click', function(){
var elmid = $(this).attr('id');
$.ajax({
type: "POST",
dataType: "json",
url: "/advertisement/activate", // URL of the Perl script
data: {adID: $(this).prev("input.ad_ID").val()},
success: function(data){
if (data.error) { // script returned error
alert("fejl");
$('div#create_createresult').text(data.msg);
$('div#create_createresult').addClass("text-danger");
$("form#createForm input#createForm_submit").removeAttr('disabled');
}
else
{
var src = $("a#" + elmid + " img").attr("src").replace("activate-16x16.png", "deactivate-16x16.png");
$("a#" + elmid + " img").attr("src", src);
$("a#" + elmid).addClass('deactivate').removeClass('activate');
$('div#create_createresult').text(data.msg);
$('div#create_createresult').addClass("success");
}
}
});
});
$("a.deactivate").on ( 'click', function(){
var elmid = $(this).attr('id');
$.ajax({
type: "POST",
dataType: "json",
url: "/advertisement/deactivate", // URL of the Perl script
data: {adID: $(this).prev("input.ad_ID").val()},
success: function(data){
if (data.error) {
alert("fejl");
$('div#create_createresult').text(data.msg);
$('div#create_createresult').addClass("text-danger");
$("form#createForm input#createForm_submit").removeAttr('disabled');
}
else
{
var src = $("a#" + elmid + " img").attr("src").replace("deactivate-16x16.png", "activate-16x16.png");
$("a#" + elmid + " img").attr("src", src);
$("a#" + elmid).addClass('activate').removeClass('deactivate');
$('div#create_createresult').text(data.msg);
$('div#create_createresult').addClass("success");
}
}
});
});
});