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

121
Views
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 answers
Answer question

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 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!