I am trying to pass specific elements from a JSON object as arguments in a JavaScript function. Here is the script I am using:
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp1=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp1=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp1.onreadystatechange=function()
{
if (xmlhttp1.readyState==4 && xmlhttp1.status==200)
{
var txttmp2 = xmlhttp1.responseText;
var obj2 = $.parseJSON(txttmp2);
var text1= document.getElementById("tabs-Mil");
var objln=obj2.post.mypost.length;
//alert(objln+"");
for(var counter=0;counter<objln;counter++)
{
text1.innerHTML+=" '"+obj2.post.mypost[counter].status+"' @ <a href='#'>"+obj2.post.mypost[counter].namalokasi+"</a> on "+obj2.post.mypost[counter].tanggal+"<br/><a href='#' onclick='addcomment("+obj2.post.mypost[counter].idpost+","+localStorage.loggeduser+")'>comment</a><hr width='80%' align='left'><br/>";
text1.innerHTML+="<span id='comments' name='comments'></span>";
}
}
}
xmlhttp1.open("GET","http://localhost:280/finaltask/forjson.php?tmpid="+iduser+"&proses=showpost",true);
xmlhttp1.send();
function addcomment(idcheckin,iduser)
{
$("#formcomment").show();
var detpost=document.getElementById("detail-post");
alert(idcheckin+"/"+iduser);
}
I have successfully passed the JSON object element using the line onclick='addcomment("+obj2.post.mypost[counter].idpost+","+localStorage.loggeduser+")'
However, when the function addcomment(idcheckin,iduser) is executed, it shows that 'idcheckin' is 'undefined'...
Can someone provide assistance with this issue?