1

I want to get the value from Apex class to my javascript. I have a value from my apex class and the value I am able to display in my VF page but not able to pass this value to my script. How can I get the vale to my script. Please help me.

1
  • As always, posting code is helpful. Without knowing where the value is in class along with your getter and setter methods, I don't know how someone can provide a useful answer. For example, the value of any variables from an inner class aren't accessible outside of a controller. – crmprogdev Oct 15 '14 at 13:12
6

You can use the merge field directly in javascript. like ,

 <script>
 function test(){
  alert('{!mergeFieldName}');
 }

you can also use tag to pass values across page and class.

If you can improve your question a bit, I can improve my answer as well.

2

To add to @akash's answer, if you are expecting it to change after a postback/reRender, the script block needs to be inside the area that is being re-rendered, or else it will not change.

Here is an example:

        <apex:pageBlock title="Images" id="ImageBlock" >
            <apex:pageBlockButtons >
                <apex:commandButton value="Refresh" action="{!refreshView}" rerender="ImageBlock"  />
            </apex:pageBlockButtons>
            <div class="imagePane">
                <apex:repeat value="{!images}" var="pic">
                        <div class="imageTile" >
                            <apex:image url="/servlet/servlet.FileDownload?file={!pic.id}" />
                            <br />
                            <apex:outputLabel >{!pic.Name}</apex:outputLabel>
                        </div>
                    </apex:repeat>
            </div>
            <script>
                 alert('{!mergeFieldName}');
            </script>
        </apex:pageBlock>

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