How can I capture the value of a variable in a for loop using an event handler?

38 views Asked by At

I'm generating this table using a for loop, and after it's generated, I want to be able to click each generated row individually so it can open a new page with detailed information of the item clicked.

The code for the table is as follows:

<fieldset>
    <table>
        <thead>
            <tr>
                <th>Codigo </th>
                <th>Nombre </th>
                <th>Lider </th>
                <th>Socios </th>
            </tr>
                <?php if(isset($nombreClub)){ for($y = 0; $y <count($nombreClub); $y++) {
                    echo  "<tr>".
                    "<td>" . $id[$y] . "</td> " . 
                    "<td>" . $nombreClub[$y] . "</td>" . 
                    "<td>" . $liderVoluntario[$y] . "</td>" .
                    "<td>" . $memberSearch[$y][0] . "</td>" .
                    "</tr>";
                }}

                ?>
        </thead>
        <tbody>
        </tbody>
    </table>
</fieldset>

Specifically, I'm trying to get the value (e.g. 1,2,3,etc) but I don't know how to code it through jquery or ajax and specifically get the value I want from [$y].

I tried to explain myself as best I could. If you need anything else regarding code or information please let me know.

1

There are 1 answers

1
Lars On BEST ANSWER

Give you TR an id and class. Put the listener on the class and use the id to redirect or load the data. This should at least get you started and pointed in the correct direction.

<fieldset>
    <table>
        <thead>
            <tr>
                <th>Codigo </th>
                <th>Nombre </th>
                <th>Lider </th>
                <th>Socios </th>
            </tr>
                <?php if(isset($nombreClub)){ for($y = 0; $y <count($nombreClub); $y++) {
                    echo  "<tr class='click_row' id='".$id[$y]."'>".
                    "<td>" . $id[$y] . "</td> " . 
                    "<td>" . $nombreClub[$y] . "</td>" . 
                    "<td>" . $liderVoluntario[$y] . "</td>" .
                    "<td>" . $memberSearch[$y][0] . "</td>" .
                    "</tr>";
                }}

                ?>
        </thead>
        <tbody>
        </tbody>
    </table>
</fieldset>

<script>
$('.click_row').click(function(){
    // Here where you will do you ajax call or what ever else
    alert(this.id);

});
</script>