Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access model attribute in jQuery

I need to pass a model attribute from controller to HTML. My application is using HTML5, thymeleaf & springboot.

I am using the below code snippet:

$(document).ready(function() {
   var modelAttributeValue = '${modelAttribute}';
}

Code snippet from my controller:

model.addAttribute("modelAttribute", "viewEmployee")

But I am not able to get the value assigned to model attribute in HTML.

Please advise.

Thank you in advance.

like image 669
Java_User Avatar asked Dec 25 '22 19:12

Java_User


1 Answers

You may want to use an inline script, like this:

<script type="text/javascript" th:inline="javascript">
    /*<![CDATA[*/

    $(document).ready(function() {
       var modelAttributeValue = [[${modelAttribute}]];
    }

    /*]]>*/
</script>

More info on script inlining here: http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#script-inlining-javascript-and-dart

like image 83
cnpfm Avatar answered Jan 11 '23 19:01

cnpfm