Reputation:
From my controller I add an Object to my model, which I can access within the Html. example
redir.addFlashAttribute("userId",userId);
return modelAndView;
Within the HTML if I access the userId
<input type="hidden" id="userId" th:value="${userId}" /> -
I can see the result. I have an external JS file, that has a function that takes as an input parameter userId. How do I read the userId directly to the JS file ? Cuz if I do it like this :
var userId=$("userId").val();
the userId doesn't take the value. It shows an empty string. How do I fix this ?
Upvotes: 2
Views: 330
Reputation: 3016
jQuery's val
method looks at the value
attribute, so it ignores th:value
. You need to use the .attr
method, e.g. $('#userId).attr('th-value')
.
Upvotes: 1
Reputation: 599
You have to use selector for id in html #userId
var userId=$("#userId").val();
Upvotes: 1