Reputation: 68
a(id='myLinkTag' onclick='myFunction(' + user._id + ')' href='') Delete
Here user._id is the variable and trying to pass that value to myFunction .
syntaxError: Invalid or unexpected token
Upvotes: 0
Views: 1235
Reputation: 3577
The problem here is that javascript is expecting quotes around functions. What jade is making is <a id='myLinkTag' onclick='myFunction(userid) href=''>
This is not what javascript is expecting, Javascript expects a valid variable name to be given to its onclick functions. There are two choices:
If you are SURE that there is NO WAY for user._id to ever be anything other than a string ONLY CONTAINING alphanumeric characters (new RegExp(/^(A-Za-z0-9)+$/)
) then put quotes in myFunction: a(id='myLinkTag' onclick='myFunction(\'' + user._id + '\')' href='') Delete
If user._id can be more than alphanumeric characters, you can instead use jade to set a class and a custom attribute like a(id='myLinkTag' class="myclass" userid=user._id href='') Delete
then in javascript you can:
:
$(".myclass").click(function(e){
var userid = $(e.target).attr("userid");
console.log(userid);
alert(userid);
});
Jade would be:
a(id='myLinkTag' class="myclass" userid=user._id href='#')
a(id='myLinkTag2' class="myclass" userid=user2._id href='#')
a(id='myLinkTag3' class="myclass" userid=user3._id href='#')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="myclass" userid="myuserid" href="#">link</a>
<a class="myclass" userid="myuserid2" href="#">link2</a>
<a class="myclass" userid="myuserid3" href="#">link3</a>
Upvotes: 3
Reputation: 1777
Use differing nested quotation marks so that you pass a string to your function.
a(id='myLinkTag' onclick="myFunction(' + user._id + ')" href='') Delete
Upvotes: 0