javascript - Calling jQuery function from jstl c:forEach ( function not defined) on page load -
i'm struggling bit joining jstl , jquery. have spring aplication provides me list, example cars: [kia, audi, fiat, mazda, opel]
i have input field if user presses button next it, adds text in form of fancy tag, or whatever. furthermore want add list elements in same form of tag while page loads. if try receive :
uncaught referenceerror: add_tag not defined (anonymous function)
so have code follows (of course strip down problem)
<script> $(document).ready(function() { function add_tag(value) { console.log("adding tag:" + value); //some styling $("#add_here").append(value); }; $("#add_tag").click(function() { add_tag($("#choosen_tag").val()); }); }); </script> <div> <c:foreach items="${cars}" var="c"> <script> add_tag("${c}"); </script> </c:foreach> </div> <input id="choosen_tag"></input> <button type="button" class="btn btn-default" id="add_tag">+</button> <div id="add_here"></div> all jquery, jstl tags etc, of course included. sole issue here calling function. tried named functions, or declaring within other var, still no success :/
minutes after posting, found solution. script should use jstl instead of other way around. adding
<script> $(document).ready(function() { <c:foreach items="${cars}" var="c"> add_tag("${c}"); </c:foreach> // rest of script adding functions ... solves issue.
Comments
Post a Comment