javascript - Select every div inside another div and check it's class with jQuery -
i have chat system , i'm showing messages using jquery + json + php. problem is: have select each div inside div "#chatmessages", contains messages, , check it's class see if exists, ajax code:
var data = {}; $.ajax({ type: "get", datatype: "json", url: "../room/a.php", async: true, cache: false, data: data, success: function(response) { if(response.error) { alert(response.msg); } else { for(var = 0; < response.length; ++) { if($("#chatscreen > *").hasclass("message" + i) == false) $("#chatscreen").append($("<div>",{ class : 'message' + response[i][1], html : response[i][0] + ' ' + response[i][4] })); } } }, error: function (response) { }, complete: function () { } });
response[i][0]
contains name, , response[i][4]
contains message. it's working fine, load function every 3000ms, repeat every message, how can check div per div searching class 'message' + response[i][1]
(that contains unique id mysql)?
i tried use ".hasclass()" doesn't work.
if want know if there's element in div given class, can do:
if ($("#chatscreen").find(".message"+response[i][1]).length > 0) {
you can refactor don't have search #chatscreen
every time through loop:
var chatscreen = $("#chatscreen"); for(var = 0; < response.length; ++) { if(chatscreen.find(".message"+response[i][1]).length > 0) { chatscreen.append($("<div>",{ class : 'message' + response[i][1], html : response[i][0] + ' ' + response[i][4] })); } }
Comments
Post a Comment