How to use Javascript in visualforce page
Here I am taking a basic example of using Javascript in a visualforce page that illustrates how to call javascript function from VF page and how to reference id of a visualforce component.
Use $component global variable to referencing DOM Id that is generated for a visualforce component.
<apex:page>
<script>
function show(tid,sec){
document.getElementById(sec).value = document.getElementById(tid).value;
}
</script>
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection id="lab" >
<apex:inputText id="name" label="AccountName"/>
<apex:inputText id="second" label="Rating"/>
<apex:commandButton value="Go" onclick="show('{!$Component.name}','{!$Component.second}')" reRender="lab"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Use $component global variable to referencing DOM Id that is generated for a visualforce component.
<apex:page>
<script>
function show(tid,sec){
document.getElementById(sec).value = document.getElementById(tid).value;
}
</script>
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection id="lab" >
<apex:inputText id="name" label="AccountName"/>
<apex:inputText id="second" label="Rating"/>
<apex:commandButton value="Go" onclick="show('{!$Component.name}','{!$Component.second}')" reRender="lab"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Comments
Post a Comment