Is it possible to call a jQuery function from within a ColdFusion switch statement? If so, how?
4 Answers
No. ColdFusion runs on the server. jQuery runs on the client (the browser). You can conditionally output (depending on which case you hit) JavaScript code that will call the jQuery function.
1 Comment
Short answer is No. As Mathew explained, CF and JS are running in separate places, and any attempt to make them both work together would be frustrating.
I for one think that there will be no reason to run JavaScript code from the server-side, as you can simply run your JavaScript code on the page load.
You might be trying to accomplish something simpler than you think, and a simple
$(document).ready(function(){ // Your code here }); Comments
Nick is certainly correct in his answer as one possible solution. That said, to me a cleaner approach might look like:
var species = '#species#'; // convert cf variable for use in javascript switch(species){ case "cat": // cat code case "dog": // dog code case "zebra": // zebra code } By using this approach, you prevent intertwining server and client code as much, thereby leading to more readable code.