Here, I want to show how to call a javaScript funtion in jQuery template using jQuery tmpl plugin.
For this we need jQuery plugin and jQuery tmpl plugin.
Here, I am checking word 'phone' of json object, if it contains returns true else false. The following is the jquery template.
<script id="myTemplate" type="text/x-jquery-tmpl">
<li>
${firstName} ${lastName},
{{if $item.myMethod() }}
my phone no. is <span>${Contact}</span>
{{else}}
my email is <span>${Contact}</span>
{{/if}}
</li>
</script>
Here the javascript function is myMethod().
The following is the binding of jquery template and json object.
$("#myTemplate").tmpl(jObj,{
myMethod: function() {
Join the conversation! Your thoughts help the community grow.