function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
pratap narwalpratap narwal 

problem in Accessing Controller variable in javascript

i am trying to get controller variable in my javascript but getting the value as spaces.

VF page
<apex:commandButton value="AddEmail" action="{!addEmail}" oncomplete="fillIn();" />   ===> calling controller method and building a string.

function fillIn() {
     alert('inside fill in');
     var customeremail = '{!finalemail}';
     alert('customeremail----'+customeremail);  ==>  controller variable  is not populating  in alert

Controller :
public String finalemail{get; set;}
public void addEmail(){
        for(SARWrapper sw : SARlist){
            if(sw.ischeck == true){
               finalemail=sw.custemail + ';';
            if (finalemail==null || finalemail =='') {
                apexPages.message msg = new ApexPages.Message(ApexPages.Severity.Fatal,' No Email is selected');
                system.debug('final email is ' + finalemail); ===> string contains valid data, trying to access in javascript.

Please advice

Thanks in Advance
Roy LuoRoy Luo
Add a hidden field to your VF page:
<apex:inputHidden id="finalEmail" value="{!finalemail}" />

function fillIn() 
     alert('inside fill in');
     var customeremail =  $('[Id$="finalEmail"]')[0].value;

Ravikant kediaRavikant kedia
HI pratap narwal,
                         There are two way to do this thing.
1) You have  to rerender your script after completion .
2) This is good way to solve your problem .

<apex:commandButton value="AddEmail" action="{!addEmail}" oncomplete="fillIn('{!addEmail}');" /> 
pratap narwalpratap narwal
Hi Roy
Thanks for the reply. But i am getting the undefined vaue while accessing the controller through hidden variable;