You need to sign in to do that
Don't have an account?

how to call javascript in an apex class
how can i call script in class?
if i have this code:
<script> function clearValue() { document.getElementById('{!$Component.page:frm:pblck:pbSectn:pbSitemLookup:accountLookup}').value = ''; document.getElementById('{!$Component.page:frm:pblck:pbSectn:pbSitemLastName:contactLastname}').value = ''; alert(); return false; } </script>
how can i call this in the controller/apex class?
Try it
<apex:page controller="calljavascript_cls" >
<script>
function func()
{
alert('function calling');
}
</script>
<apex:outputText value="{!callfunc}" escape="false"></apex:outputText>
</apex:page>
-------- apex class --------------
public class calljavascript_cls
{
public string callfunc{get;set;}
public calljavascript_cls()
{
callfunc='<script> func(); </script>';
}
}
All Answers
You can't call JavaScript from the controller, the page is the output of the controller, rather than dynamically interacting with it. What you can do is set a property in the controller, so that when the page is rendered the javascript executes.
Can you explain a little more about what you are trying to achieve?
i am trying to clear a lookupfield value when the other field is equal to null
this is the code that i'm calling here(<apex:inputField value="{!leave.Project_1__c}" onclick="{!Approver1}">)
do you know how can i clear the field?
thanks.
Doing this kind of thing in a getter is problematic, as the order of execution isn't guaranteed so the getter for the lookup may execute before the getter that decides to clear it.
Does the user make the selection of the other field and then post the form back to the controller?
Try it
<apex:page controller="calljavascript_cls" >
<script>
function func()
{
alert('function calling');
}
</script>
<apex:outputText value="{!callfunc}" escape="false"></apex:outputText>
</apex:page>
-------- apex class --------------
public class calljavascript_cls
{
public string callfunc{get;set;}
public calljavascript_cls()
{
callfunc='<script> func(); </script>';
}
}