Elizabeth
Elizabeth

Reputation: 173

javascript onclick function not working

So I adapted this code from another function that does work and is used on the same page. I don't know if that could do anything to it, so I thought I'd mention that, and also that both have id="replace" in them, which I don't know if it could interfere.

Anyway, the code to call the function is this:

 if (mysql_num_rows($LIKE) != 0)

{

  echo "
        <td id=\"replace\" style=\"text-align:left\">".$like." Likes -- <a style=\"color:#234904\" onmouseover=\"this.style.color='#6666CC'\" onmouseout=\"this.style.color='#234904'\" id=\"" . $POST->id . "\" onclick=\"likePost(this.id)\" name=\"like\">Like This Post</td>
        ";
}

the javascript function is defined as this:

var xmlHttp;

function likePost(x)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request");
 return;
 }

var pid = document.getElementById(x).id;
var url="forum-like.php";
url+="?pid="+pid;
url+="&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

when I click on what should call the function, nothing happens. If I execute the script using the php page called in the js function, that works fine though... I don't know if I missed anything or anything is wrong though... I also know that the $POST->id part does call the right id... hope this is enough info, otherwise let me know and I'll edit in more :D Thanks for any help!

Upvotes: 0

Views: 334

Answers (2)

Aaron W.
Aaron W.

Reputation: 9299

var pid = document.getElementById(x).id

You're already passing the id so just use

var pid = id;

If you're still having issues then use firebug or your browser's developer tools to get the actual javascript error.

Upvotes: 1

christurnerio
christurnerio

Reputation: 1469

It looks like you are passing this.id to the function, but then trying to use that (x) to then look up the id again as if that were a DOM object.

If you leave the php code as is, you can just change your javascript method to say:

url+="?pid="+x;

And you can get rid of: (since you are already passing the id and don't need to retrieve it)

var pid = document.getElementById(x).id;

Hope that works for you!

Upvotes: 0

Related Questions