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

123
Vistas
Attribute from php to Jquery.click within php foreach

I've been trying out different things for a while, but nothing seems to be working. And would probably need another kind of perspective.

For the question: I got a php foreach loop going, and every object is posting the ID of that object.

But my problem in this that when i click, the object is giving me the last number of the foreach. Ive been trying to post the value in JSON, and just plain php.

PHP

<?php
foreach ($places as $place) {
    $place_id = json_encode($place['id']);
    echo "<div class='placeContainer' onClick='openMessage(\'' . $place_id . 
'\')'>";
?>

Jquery

$(document).ready(function () {
    $('.placeContainer').click(function () {
        $(this).attr(phpvar);
        var myKey = JSON.parse(phpvar);
        console.log(myKey);
    })
});

Thanks for the help!

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Try this :

PHP Code :

<?php
foreach ($places as $place) {
    $place_id = json_encode($place['id']);
    echo "<div class='placeContainer' data-id='".$place_id."'>";
?>

JS Code :

$(document).ready(function () {
    $('.placeContainer').click(function () {
        var myKey = $(this).attr('data-id');
        console.log(myKey);
    })
});
over 4 years ago · Santiago Trujillo 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