0

I'm trying to select a particular row using jquery from a looped foreach. For now I'm just using a simple alert to see if I get the right row. Problem is, it only selects the last row, and not the row I clicked.

Example code:

     <?php foreach ($customers as $c) : ?>
            <tr>
                <td><?php echo $count++; ?></td>
                <td><?php echo $c['firstname'] . " " .  $c['lastname']; ?></td>
                <td><?php echo $c['phone']; ?></td>
                <td><?php echo $c['email']; ?></td>
                <td style="display:none" id="'<?php echo $c['id'] ?>'"> </td>
            </tr>
    <?php endforeach ?>

The Jquery call:

    $('tr td ').click(function(){
    var fn = '<?php echo $c['firstname']; ?>';
    alert(fn);}
    );
AD6
  • 57
  • 1
  • 7
  • This is the [**infamous loop issue**](http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example), only in PHP – adeneo Mar 07 '15 at 16:48
  • 2
    The real question is, what the heck is that PHP doing in the javascript, why not `$(this).text()` – adeneo Mar 07 '15 at 16:48
  • @adeneo - thanks for your reply. I was able to get a value from the row. But how would I get all values instead of just one element. I would really like to get the hidden field which represents the user in the database. – AD6 Mar 07 '15 at 16:56

2 Answers2

0

You can access particular row's column like bellow

$('tr.item').each(function() { // selected tr 
        var col1 = $(this).find("td:eq(0) > input").val(); // find the   input field value inside the first column of the row       
        var col2 = $(this).find("td:eq(1) > input").val();}
rdanusha
  • 913
  • 3
  • 15
  • 24
0

This will output all td values. Credits

<html>

    <head>
        <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" type="text/css" rel="stylesheet" />
    </head>
    <body>

<?php

    $count = 0;
    $customers = array (
                        array('firstname'=>'fname1', 'lastname'=>'lname1','phone'=>'111','email'=>'a@gmail.com','id'=>1),
                        array('firstname'=>'fname2', 'lastname'=>'lname2','phone'=>'222','email'=>'b@gmail.com','id'=>2),
                        array('firstname'=>'fname3', 'lastname'=>'lname3','phone'=>'333','email'=>'c@gmail.com','id'=>3),
                        array('firstname'=>'fname4', 'lastname'=>'lname4','phone'=>'444','email'=>'d@gmail.com','id'=>4),
    );

    ?>
    <table class="table table-bordered">
     <?php foreach ($customers as $c) : ?>
            <tr class="getdetails">
                <td><?php echo $count++; ?></td>
                <td><?php echo $c['firstname'] . " " .  $c['lastname']; ?></td>
                <td><?php echo $c['phone']; ?></td>
                <td><?php echo $c['email']; ?></td>
                <td style="display:none" id="'<?php echo $c['id'] ?>'"> </td>
            </tr>
    <?php endforeach ?>
    </table>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script>
    $(".getdetails").click(function(){
         $(this).find("td").each(function(){
            console.log($(this).html());
        });
    });
</script>
    </body>
</html>
Community
  • 1
  • 1
Alaksandar Jesus Gene
  • 6,523
  • 12
  • 52
  • 83