phpjqueryjqmodal

Call jQuery for each row


I want to make a link to call for each row.

Here is the code:

foreach($docs as $row) {
echo "<td><h5><a href='' id='onclick' class='onclickcalldocEdit_".$row->dId."'>".$row->dName."</a></h5></td>";
echo "<div id='response_proj' class='container_proj_".$row->dId."'>
/* container code here */
}

So, basically I create a container for each row and name differs only by id. I've used this script to try to get it work:

$(document).ready(function(){
    var className = $('#onclick').attr('class');
    var contName = $('#response_proj').attr('class');
    $(className).jqm({trigger:contName, toTop: true});
});
</script> ";

Is it even possible?


Solution

  • Try this:

    $(document).ready(function(){
      $("#onclick").each(function(){
        var className = this.attr('class');
        var contName = this.closest('td').next('#response_proj').attr('class');
        $(className).jqm({trigger:contName, toTop: true })
      })
    })
    

    Frankly speaking, I think the better way is to use data-id and class attributes(without id):

    <a href='' class='onclick' data-id='".$row->dId."'>".$row->dName."</a>
    

    And access id via

    var id = $('.onclick').data('id');