3

Is it possible to call a jQuery function from within a ColdFusion switch statement? If so, how?

1
  • Could you elaborate a bit on how you want the CF logic to affect the call to jQuery? Commented Mar 1, 2010 at 22:22

4 Answers 4

6

You do know that you are mixing client and server side code? That said it is really not an issue to do:

<script language="javascript"> <cfswitch expression="#n#"> <cfcase value="test1"> $.something1() </cfcase> <cfcase value="test2"> $.something2() </cfcase> </cfswitch> </script> 
Sign up to request clarification or add additional context in comments.

Comments

3

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

Right, I'd want it to run the jQuery function after the page renders.
2

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

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.