Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

122
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda