简体   繁体   中英

passing parameters to javascript function through OnClick html attribute?

I have this error which is bugging me for a while now and I cant figure out how to fix it. It's probably something really stupid that I have overlooked and cant spot it.

but say I have this link on my page.

<a class=\"subbotbtn\" onclick=\" javascript:postdislike($pid, $username); \" href=\"#\"><img class='likeimg dislikeimg' src='images/dislike.png' /><span style=\"color:blue;\" class='reacttext'>Dislike</span></a>";

When I click it, its supposed to go to the javascript file and use this function here:

function postdislike(pid, user) {
"use strict";
alert("yay");
}

When I click, the alert never happens. Instead I end up getting this error:

VM85525 Ram:126 Uncaught ReferenceError: Ram is not defined onclick @ VM85525 Ram:126

"Ram" is the username I am passing through. pid (post id) is another variable im passing through as a number (in this case, 60).

Can anyone please tell me what im doing wrong?

EDIT: also it might be helpful to add that this link is inside php echo statements, hence the extra slashes.

fixed

<a class=\"subbotbtn\" onClick=\" postdislike('$pid', '$username'); \" href=\"#\"><img class='likeimg dislikeimg' src='images/dislike.png' /><span style=\"color:blue;\" class='reacttext'>Dislike</span></a>";

had to add single quotes around the parameters...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM