JQuery not working with ajax?
I have a dynamic div that will change based on json data. I'm trying to control the content of this div using jquery inside a for loop, but it doesn't work?
Can anyone give me a clue and some idea on how to fix the code?
$.ajax({
type: "POST",
url: 'http://192.168.12.230/XXXX/XXX',
dataType: 'json',
contentType: "application/json",
success: function (data) {
console.log(data);
var projectList = "<ul style='list-style:none;'>"
for (var i = 0; i < data.d.length; i++) {
projectList += "<li>" +
"<div class='location'>" +
"<p>Sounth Africa</p>" +
"</div>" +
"<div class='status'>" +
"<div class='status_circle'></div>" +
"<p>in raising</p>" +
"</div>" +
"</li>"
$(".category p").append(data.data[i].categoryName);
if (data.data[i].raisingStatus == 1) {
$(".status p").html("in raising");
$(".status_circle").css("background-color", "#32C832");
} else if (data.data[i].raisingStatus == 0) {
$(".status p").html("stop raising");
$(".status_circle").css("background-color", "#CD3C14");
}
}
projectList += "</ul>";
$('#projectList').append(projectList);
},
error: function () {
alert("error");
}
});
By the way, instead of importing HTML into javascript, is there any idea to implement this functionality without using HTML code?
Thank you so much:)
source to share
if (data.data[i].raisingStatus == 1) {
$(".status p").html("in raising");
$(".status_circle").css("background-color", "#32C832");
} else if (data.data[i].raisingStatus == 0) {
$(".status p").html("stop raising");
$(".status_circle").css("background-color", "#CD3C14");
}
you cannot populate the dom $(".status p")
before adding to the dom.$('#projectList').html(projectList);
you can add css and text with html code like this.
if (data.data[i].raisingStatus == 1) {
projectList += "<div class='status'>in raising</p>";
var style = "background-color:#32C832";
projectList += "<div class='status_circle' style='"+style+"'></div>";
} else if (data.data[i].raisingStatus == 0) {
projectList += "<div class='status'>stop raising</p>";
var style = "background-color:#CD3C14";
projectList += "<div class='status_circle' style='"+style+"'></div>";
}
For your second question, yes, it's all useless with html js and css. You can create an html variable and replace it with parameters with cleaner code.
for (var i = 0; i < data.data.length; i++) {
var status = "";
var style = "";
if (data.data[i].raisingStatus == 1) {
status = "in raising";
style = "background-color:#32C832";
} else if (data.data[i].raisingStatus == 0) {
status = "stop raising";
style = "background-color:#CD3C14";
}
var projectList="<li><div class='location'><p>Sounth Africa</p></div><div class='status'>
<div class='status_circle' style='{0}'></div><p>{1}</p></div></li>".format(style, status);
}
String.prototype.format = function() {
var formatted = this;
for (var i = 0; i < arguments.length; i++) {
var regexp = new RegExp('\\{'+i+'\\}', 'gi');
formatted = formatted.replace(regexp, arguments[i]);
}
return formatted;
};
source to share
if (data.data[i].raisingStatus == 1) {
$(".status p").html("in raising");
$(".status_circle").css("background-color", "#32C832");
} else if (data.data[i].raisingStatus == 0) {
$(".status p").html("stop raising");
$(".status_circle").css("background-color", "#CD3C14");
}
* These conditionals should give a different loop.
$ (". status p") . This line will be empty before the creation that you accessed in the same loop.
"<div class='status'>" +
"<div class='status_circle'></div>" +
"<p>in raising</p>" +
"</div>"
Making a div status when created in a loop where you are not trying to access in the same loop. Try accessing $ (". Status p") in a new loop. It will work.
source to share