Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Is there anyway to assign data to multiple javascript variables? I am trying to trigger something onblur which would change some other fields contained within the VF Form. They will go on their lookup field, then click the content then it will fill content in a SELECT.

I am wondering if you could send references/pointers of the variables to the controller, so it can assign the variables there and send it back. Or am i forced to have multiple functions with multiple returns to try and get it done.

I know about remoteActions but that will only give me back one return. Just looking to get it done in one function instead of multiple ones.

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

Totally! Whenever you use a formula in a Visualforce page, {! ... }, Apex calculates the formula, and writes the result right into the DOM. So to set a variable in Javascript:

<apex:page standardController="Account" extensions="getContactsExtension" ...>
    ....

    <script>
        var accountId='{!Account.id}';  // NOTE: I wrapped the value in quotes so Javascript interprets it as a string
        var ContactArray = {!ContactList};

        alert('The Account Id = '+accountId);

        // etc....
    </script>
</apex:page>
share|improve this answer
    
Unfortunately this doesn't work, this is what i was hoping for. But it seems like it doesn't work. I assign the Variable in controller then in js i use your syntax. String comes back blank. –  James Feb 28 at 19:59
    
try console.log(yourVar) or alert(yourVar) and see that it works. Try adding the alert statement as I did in my updated solution –  amatorVitae Feb 28 at 20:00
    
I understand account working because you have the main controller available for it however, I have a variable defined in my controller that is String Payment{get; set;} I assign the variable using an actionFunction, then i try and get the string using var x = {!Payment}; to no avail blank on the alerts. –  James Feb 28 at 20:03
1  
well, if you use an Action Function, you are probably doing AJAX requests and changing the DOM. Make sure you rerender your script! (Wrap your <script> block with an <apex:outputPanel> and rerender that Panel in your Action Function) –  amatorVitae Feb 28 at 20:05
    
That might be it! Thanks let me try this again! –  James Feb 28 at 20:06
add comment

Remote Action methods return variable can be of type list or custom wrapper class as well.So you can return as a list instead of one variable .

share|improve this answer
    
Great thanks i realized this after i could have just made a list and send what i need with it. Would you happen to know if ActionFunctions can return variables? –  James Feb 28 at 16:07
    
Please upvote and accept if that helps .Also Action Function cant but you can use getters and setters and use getter to return data to javascript too –  Mohith Shrivastava Feb 28 at 16:11
    
@James can you accept Mohith answer and upvote if it helps you. He answered correctly and clearly. –  Baskaran S Feb 28 at 17:28
2  
isn't the question how to "Assign Data to JavaScript Variable from Controller"? just sayin @baskaran –  amatorVitae Feb 28 at 20:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.