Reputation: 2037
I'm trying to create a custom tag that includes jQuery code.
For example a custom tag like <i2:myTag id="myId">
that turns into something like:
<a href="#" id="myId">Click</a>
<script>
$("#myId").click(function(){...})
</script>
That is not working because as I am using the resourses plugin to load the jQuery library, at the time of being processed the script code, jQuery is not yet loaded.
I guess I could use document.ready or window.load to force the previous script code to be executed at the right moment but, is there any more elegant way to achieve it?
For example, one solution that would be enough for my needs is calling the resources plugin from within my custom tag. But I don't know how to pass the string with the javascript code as the body closure to the gsp <r:script>
tag:
myTag={attrs,body->
out<<"""<a href="#" id="myId">Click</a>"""
def javascriptCode = """$("#myId").click(function(){...})"""
r.script( ??? ) <--- how to set the javascriptCode as the body closure of the <r:script> tag?
}
Any idea is appreciated.
Thanks
Upvotes: 0
Views: 156
Reputation: 24776
Actually it would be much simpler to just use the $(document).ready(); feature of jQuery.
So for your example just make it render:
$(document).ready(function() { $("#myId").click(function(){...}); });
This seems like the very simplest solution, and thus the most elegant. Why over design something that is going to just add complexity when this works just fine? Sorry if I missed what the issue is with using this but I really do think this is the simplest solution.
Upvotes: 1