How to run JavaScript on render of a panel in Visualforce Page Salesforce ?

If you are a salesforce developer and using Javascript also in your visualforce page then sometimes you might need to trigger your JavaScript function on some events. 

In this article I will explain you to trigger your JavaScript function on Rendered of a output panel.

To this first write your Javascript function like for example here is my javascript function below :  

1
2
3
function testMe(){
alert('I am working');
}

As you know that you can use this javascript in head tag and call it in button's click. But what if you need to call it when a panel is rendered.

For this just copy your javascript code before closing tag of that panel as shown below :  

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<apex:outputPanel rendered="{!renderMe}" >
                                    <h3 class="register-heading">Heading</h3>
                                    <br/>
                                    <p>
                                        Testing text
                                    </p>
                                    
                                    <script>

                                    function testMe(){
                                          alert('I am working');
                                    }

                                    </script>
</apex:outputPanel>

So in above code I am rendering the outputPanel on click of a button than with output panel my javascript will automatically triggered.


 If you have any question please leave a comment below.
If you would like to add something to this post please leave a comment below.
Share this blog with your friends if you find it helpful somehow !

Thanks
Keep Coding 

Post a Comment

2 Comments