msaggar msaggar - 4 months ago 34
Javascript Question

Passing a field of a domain object into javascript function in grails

I want to pass a field of a domain object to a javascript function in my view.gsp (grails) , but I am getting a syntax error.

Here is my gsp and javascript - please let me know if you see the syntax error. Thanks!

/*HTML*/
<td><a href='#' data-toggle="popover" id="popoverID" onclick="function setID( ${studentInstance.id})">${fieldValue(bean: studentInstance, field: "active")}</a></td>

/*JS*/
<script type="text/javascript">
var id = 0;
function setID(userId){
console.log("userId: " + userId);
id = userId;
}
</script>

Answer

The issue is you have function in your onclick. You don't need it there. Remove it so your onclick looks like this:

onclick="setID( ${studentInstance.id})"

Comments