$finalval=0;
while($row = mysql_fetch_array($result))
{
$finalval=$finalval. "<a href='#'
onClick='showContent(".json_encode($row['ID']).")'>". $row['Title'] . "</a> <br>" ;
}
echo $finalval;
<script language="javascript" type="text/javascript" >
function showContent(value)
{
alert(value);
}
</script>
Please rectify my error alert box is displaying null instead of ID value.Thanks
You don't need to Json_encode just
'showContent(".$row['ID'].")'
I would also suggest you to check your source code in your browser if this doesnt work and see what it prints in showcontent(). I think you are not getting any value in $row['ID']. Again check source code in your browser by right clicking in browser and clicking view source code and check there your showcontent() function and see what value is there
When you call showContent()
, you have to put the ID you want to show between ", like this:
$finalval=$finalval. "<a href='#' onClick='showContent(\"".json_encode($row['ID'])."\");'>". $row['Title'] . "</a> <br>"
Try this one
<?PHP
$result = mysql_query($query);
$finalval = 0;
while ($row = mysql_fetch_array($result)) {
$finalval = $finalval."<a class='show-content' data-id='". $row['ID'] ."' href='#' >". $row['Title'] ."</a> <br>" ;
}
echo $finalval;
?>
<script language="javascript" type="text/javascript" >
var element = document.querySelectorAll(".show-content");
for (var link in element) {
element[link].onclick = function() {
showContent(this.getAttribute('data-id'));
};
}
function showContent(value){
alert(value);
}
</script>