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".