access controller variable in javascript

  • Is there any way to access controller variable set in a Method in Javascript. In the example above the value is set in constructor. But when I set it in a method it is showing as NULL in Javascript

    In controller:

    public class abc {
        public string varA { get;set; }
        public void fn(){
            varA='hello';
        }
    }
    

    In VFPage:

    function hello(){
        var bool = '{!varA}';
        alert(bool);
    }
    

    The alert statement in the VF Page(i.e bool) is displayed as null. Can you please help how to access a variable set from a method instead of constructor?

    Has method fn() been invoked by the time you need it in your Javascript? When does fn() get invoked?

    yes fn() is invoked first by the time we need it in javascript

  • Mark Pond

    Mark Pond Correct answer

    8 years ago

    Accessing controller properties uses the usual get & set syntax.

    Set in a constructor and retrieved using the shorthand notation

    public class YourController {
        public string varA { get; set; } // use the proper type
    
        public YourController() {
            varA = 'Some text';
        }   
    }
    

    or

    Retrieved from the getNNN mechanism

    public class YourController {
        public YourController() { }
    
        public string getvarA() {
          return 'Some text';  
        } 
    }
    

    or

    Retrieved from a shorthand getter which calls a method in the controller

    public class YourController {
        public YourController() { }
    
        public string varA { get { return doIt(); } }
    
        private string doIt() {
            return 'Some text';
        }
    }
    

    VF Page - JavaScript function - controller property reference will work with any of the above examples:

    <script>
        function hello(){
            var theControllerValue = '{!varA}';
            alert(theControllerValue);
        }
    </script>
    

    The rendered source for the page, which you can go look at in the browser, will look like this after the substitution for the controller variable has been made:

    function hello(){
        var theControllerValue = 'Some text';
        alert(theControllerValue);
    }
    

    I need string in javascript not boolean

    @sf.dev, updated the example per your comment but the implementation is identical in both cases.

    @ Mark Pund variable is showing null in javascript

    @MarkPond You apparently missed the part where they asked "not in the constructor." This entire answer, while awesomely detailed, doesn't appear to answer the question at all.

    @sfdcfox there are two examples in my answer which are "not in the constructor" and demonstrates the implementation in three ways.

    @sfdcfox I now see where the disconnect is - the original question makes no mention of whether the 'method', `fn()` in the example, is called by a page action like a button click as your example demonstrates. My examples presume that this is not the case. You are correct that there are a variety of ways to implement the solution. Additional clarification in the question of the page behavior and how the method is called would help.

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM