1👍
✅
it’s a little odd that the json object property is different between calls. but, with that in mind… here you go
success: function (data) {
if(data.green === "green"){
$("#myDiv").addClass("myClass");
}
if(data.red === "red"){
$("#myDiv").addClass("myOtherClass")
}
if(data.default === "default"){
$("#myDiv").addClass("myDefaultClass")
}
},
0👍
If you have control over the structure of your JSON object outputted by the Django function I’d suggest to form the JSON in a way that lets you keep the key fixed and the value variable, in your case the key would be color and the variable would be one between green, red and default
{color: 'green||red||default'};
and then in your AJAX function just switch(data.color)
like this:
switch(data.color){
case "red": $('#yourDiv').addClass('red');break;
case "green": $('#yourDiv').addClass('green'); break;
default: $('#yourDiv').addClass('default'); break;
}
If you cannot choose how to build you JSON then you can just use some if statements
if(data.green === "green"){
$('#yourDiv').addClass('green');
}
if(data.red === "red"){
$('#yourDiv').addClass('red');
}
if(data.default === "default"){
$('#yourDiv').addClass('default');
}
- Django FormSetView with custom validation
- Loading django.apps module inside the url patterns file
- Cant disable Button using jquery in Django
Source:stackexchange.com