Alex Rivera | Logout

How do you access a model attribute in jquery?

Asked 2011-02-16T19:50:32.500
13

I'm adding an object to my ModelAndView in spring and forwarding to my jsp view. I need to access that object in my jquery. Is this possible without first putting the value in a hidden field? How is it done?

Edit
Report

2 Answers

29
<script type="text/javascript">
   var modelAttributeValue = '${modelAttribute}';
</script>

This will resolve the model attribute added by model.addAttribute("modelAttribute", value)

answered 2011-02-16T20:02:49.357
4

probably, you can save the model attribute in a hidden field and access it onload as below.

$(document).ready(function(){
  var modelAttr = $("#modelAttr").val();
  alert(modelAttr);
}

input type="hidden" id="modelAttr" name="modelAttr" value="${modelAttribute}"/>

Add c:out around the ${modelAttribute} in the jsp.

answered 2011-06-01T09:29:13.100

Your Answer