I am facing a problem with jQuery code. The problem is I am designing one chat application. Here the users list will get from the backend through ajax and need to be display in the frontend. I called the ajax I am getting the data I am able to display the data. Here I am creating a element as following:
function display_users_list(html) {
var users_list = JSON.parse(html);
var display_users = '';
for (var key in users_list) {
display_users += '<div class="user user-list" data-user-id='+users_list[key].userid+'>';
display_users += users_list[key].username;
display_users += '</div>';
}
$(".chat-body").html(display_users);
}
But the problem is when I created a element using jquery with css class I am unavailable to call that css class in same jquery file. I am pasting the code may be you guys will understand my problem. I am calling the css as following:
$(".chat-body > .user-list").each(function() {
$(this).on("click", function() {
var uId = $(this).data('user-id');
var innerTxt = $(this).text();
console.log("uid:"+uId+"innerTxt:"+innerTxt);
//$(".chat-box").after(generate_msg_box(uId, innerTxt))
});
});
Entire code is following: html code:
<div class="chat-box">
<div class="chat-head">Users List</div>
<div class="chat-body">
No User Found
</div>
</div>
<div class="msg-box">
<div class="msg-head">
User 1
<div class="close-msg">x</div>
</div>
<div class="msg-wrap">
<div class="msg-body">
<div class="sender-msg">This is sender message</div>
<div class="recevier-msg">This is recevier message</div>
<div class="sender-msg">This is sender message</div>
<div class="recevier-msg">This is recevier message</div>
<div class="sender-msg">This is sender message</div>
<div class="recevier-msg">This is recevier message</div>
<div class="recevier-msg">This is recevier message</div>
</div>
<div class="msg-footer">
<textarea class="msg-input">Sample</textarea>
</div>
</div>
</div>
jquery code:
$(document).ready(function() {
$(".msg-box").hide();
get_users_list();
$(".chat-head").click(function() {
$(".chat-body").slideToggle("slow");
});
function get_users_list() {
$.ajax({
url: "phpActions/actions.php",
method: "post",
data: "actions=getData",
success:function(html) {
display_users_list(html);
}
});
}
function display_users_list(html) {
var users_list = JSON.parse(html);
var display_users = '';
for (var key in users_list) {
display_users += '<div class="user user-list" data-user-id='+users_list[key].userid+'>';
display_users += users_list[key].username;
display_users += '</div>';
}
$(".chat-body").html(display_users);
}
$(".chat-body > .user-list").each(function() {
$(this).on("click", function() {
var uId = $(this).data('user-id');
var innerTxt = $(this).text();
console.log("uid:"+uId+"innerTxt:"+innerTxt);
//$(".chat-box").after(generate_msg_box(uId, innerTxt))
});
});
});