Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

142
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda