1

I have records that are being looped through and sent to the html file via json_encode. For each record, I have a data-attribute that I am storing the record id. What I am trying to accomplish is when the user clicks "Edit" (.recEdit) then I can obtain the id (data-recId). I am trying to do this with a click function, utilizing the $(this) and parent() functions.

What am I doing wrong?

Here is the looped code from the php file:

$html .= '<div class="recentProjectCont" data-recId="'.$recProjId.'">';
$html .= '<div class="recProjInfoCont">';
$html .= '<div class="recInfoCont1">';
$html .= '<span class="recProjName recBaseFormat">'.$recProjName.'</span>';
$html .= '</div>';
$html .= '<div class="recInfoCont2">';
$html .= '<span class="recInfoStat recBaseFormat">'.$recProjStat.'</span>';
$html .= '</div>';
$html .= '</div>';
$html .= '<div class="recEdit">Edit</div>';
$html .= '</div>';

Then the click function where I am attempting to obtain the id.

$(document.body).on('click', '.recEdit' ,function() {
    var projID = $(this).parent().data('recId');
    console.log('Project ID is..... ' + projID);
});
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Paul
  • 3,348
  • 5
  • 32
  • 76

3 Answers3

1

The names of data attributes get normalised to lowercase in jQuery's internal cache, so you need to use data('recid') instead of data('recId')

$(document.body).on('click', '.recEdit', function() {
  var projID = $(this).parent().data('recid');
  console.log('Project ID is..... ' + projID);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="recentProjectCont" data-recId="$recProjId">
  <div class="recProjInfoCont">
    <div class="recInfoCont1">
      <span class="recProjName recBaseFormat">$recProjName</span>
    </div>
    <div class="recInfoCont2">
      $html
      <span class="recInfoStat recBaseFormat">$recProjStat</span>
    </div>
  </div>
  <div class="recEdit">Edit</div>
  $html
</div>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0

If you don't stick to using $(this), you can try to provide onClick handler function like this:

function(event) {
  var projID = $(event.target).parent().data('recId');
  console.log('Project ID is..... ' + projID);
}
Ogochi
  • 320
  • 3
  • 12
-2

You can change a little your approach in this way - change before last php line to

$html .= '<div class="recEdit" onclick="clicked('.$recProjId.')">Edit</div>';

Then remove jquery handler and add simple

function clicked(projID) { 
    console.log('Project ID is..... ' + projID);
}

function clicked(projID) {
  console.log('Project ID is..... ' + projID);
}
.recEdit{ padding: 10px; background: #fdf; width: 30px; cursor:pointer }
<div class="recentProjectCont" data-recId="666">
  <div class="recProjInfoCont">
    <div class="recInfoCont1">
      <span class="recProjName recBaseFormat">My-project</span>
    </div>
    <div class="recInfoCont2">
      <span class="recInfoStat recBaseFormat">stat_123</span>
    </div>
  </div>
  <div class="recEdit" onclick="clicked(666)">Edit</div>
</div>
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345