Get number of the id attribute in an img tag within the while loop

0

The problem is this: I have a while loop in PHP and I need to fetch the value of a id attribute inside a img tag. This value is dynamic because it assigns it a PHP variable.

$getLastFour = mysqli_query($dbc,"Select * From products Order By id_product DESC LIMIT 4") or die(mysqli_error($dbc));
while($row = mysqli_fetch_array($getLastFour)){
        $idProd = $row["id_product"];
        $name = $row["name_Product"];
        $price = $row["prod_price"];
        $description = $row["prod_description"];
        $idImage = $row["img_id"];

?>
                            <div class="b-wrapper"><h2 class="b-animate b-from-left b-delay03 ">
                                <img src="images/link-ico.png" class="img-responsive" id="<?php echo $idImage ?>"  alt="" onclick="showId();"/></h2>
                            </div>
<?php
        }
        ?>
        <script type="text/javascript">
            function showId () {
                var id = document.getElementById('<?php echo $idImage ?>');
                var nodeList = [];
                for (var i = 0; i < id.length; i++) {
                    nodeList[i] = id[i];
                }
                alert(nodeList[i]);
            }
        </script>

The problem is how id will receive different values, when fetching the value with javascript this returns "undefined".

    
asked by anonymous 30.09.2016 / 17:53

1 answer

1

Just pass the element as a method parameter:

function showId(el) {
  alert(el.id);
}
<img src="http://aux.iconpedia.net/uploads/iphone-bk-icon-64.png"id="um" onclick="showId(this);">


<img src="http://aux.iconpedia.net/uploads/1971286922493102958.png"id="dois" onclick="showId(this);">


<img src="http://aux.iconpedia.net/uploads/16965988981513619283.png"id="tres" onclick="showId(this);">
    
30.09.2016 / 18:01