Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

148
Views
How can i display massages based on the user who sent and received the message

I don't know what's wrong with my code am not getting the intended output. Anybody to lend a helping hand and I'll really appreciate it.

If append the row class inside the for loop am getting all the messages but you can't differentiate who sent and who received and when I append the class outside the for loop am only getting one message.

xhr.onload = function () {
  var messages = [
    { id: 36, message: "hay here", username: null, senderId: 1, receiverId: 9 },
    { id: 38, message: "hay there again", username: null, senderId: 1, receiverId: 9 },
    { id: 37, message: "yes  hay", username: null, senderId: 9, receiverId: 1 },
  ];
  
  var rowClass = "";

  for (var message = 0; message < messages.length; message++) {
    if (messages[message].senderId === messages[message].senderId) {
      rowClass =
        '<div style="text-align:right">' +
        '<p style="background-color:lightblue">' +
        mymessages[message].message +
        "</p>" +
        "<div>";
    } else {
      rowClass =
        '<div style="text-align:left">' + '<p style="background-color:green">';
      messages[message].message;
      "</p>" + "<div>";
    }
  }
  $(".message").append(rowClass);
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need to check whether the current user's id is same as that of the message, for example:

const currentUserId = 9;

var messages = [
  { id: 36, message: "hay here", username: null, senderId: 1, receiverId: 9 },
  { id: 38, message: "hay there again", username: null, senderId: 1, receiverId: 9 },
  { id: 37, message: "yes  hay", username: null, senderId: 9, receiverId: 1 },
];

var rowClass = "";

for (var message = 0; message < messages.length; message++) {
  if (messages[message].senderId === currentUserId) {
    rowClass +=
      '<div style="text-align:right">' +
      '<p style="background-color:lightblue">' +
      messages[message].message +
      "</p>" +
      "<div>";
  } else {
    rowClass +=
      '<div style="text-align:left">' +
      '<p style="background-color:green">' +
      messages[message].message +
      "</p>" +
      "<div>";
  }
}

$(".message").append(rowClass);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="message"></div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!