+ Start a Discussion
christwin123christwin123 

Setting a Boolean value for a variable inside a Java Script in an Apex Page

Hi,

 

I want to set the value of a Rerender variable inside a Java script in an apex page. I give that as below bot it is not working right.

 

<script>

function earth(){
    {!Homeselected} = False;
           }
 </script>

 

I call the function from an HTML Href Link.

<li><a href=\'/apex/Home?isdtp=vw\' target=\'iframe_a\' onclick="return earth();">Home</a></li>

 

But I am not able to set that variable.Could someone please guide me in setting the value.

 

Thanks in Advance.

Christwin

Best Answer chosen by Admin (Salesforce Developers) 
SarfarajSarfaraj

Use apex:actionFunction. Insert the following code inside an apex:form element anywhere in your VF page.

 

<apex:actionFunction name="setHomeSelected" reRender="none">
     <apex:param name="x" value="x" assignTo="{!HomeSelected}"/>
</apex:actionFunction>

 Now you will be able to set the value from JavaScript, like this,

 

<script>
function earth(){
    setHomeSelected(false);
}
</script>

 Things to note:

1. You must have public setter method defined in your constructor, like this:

 

public Boolean HomeSelected{set;}

or

Boolean HomeSelected;
public void setHomeSelected(Boolean value){
     this.HomeSelected = value;
}

 2. Call to setHomeSelected from JavaScript is asynchronous. So if you need any JavaScript operation after this call you have to use callback mechanism. Like this,

 

<apex:actionFunction name="setHomeSelected" reRender="none" oncomplete="myCallbackMethod();">
     <apex:param name="x" value="true" assignTo="{!HomeSelected}"/>
</apex:actionFunction>

Now if you have a JavaScript method defined, it will be called once the setter of HomeSelected returns.

 

<script>
function myCallbackMethod(){
alert("hi");
}
</script>

3. Rerender attribute in actionfunction declaration signifies that it is one Ajax request and the associated component will get rerendered after completion of the call. If you don't use this attribute, it will be one post request and your page will get refreshed. I have used rerender="none". This one I use when I need to do asynchronous request and I don't have anything to rerender. This will work normally provided you don't have any component with ID "none". If you have one, please use something else instead of none.

All Answers

SarfarajSarfaraj

Use apex:actionFunction. Insert the following code inside an apex:form element anywhere in your VF page.

 

<apex:actionFunction name="setHomeSelected" reRender="none">
     <apex:param name="x" value="x" assignTo="{!HomeSelected}"/>
</apex:actionFunction>

 Now you will be able to set the value from JavaScript, like this,

 

<script>
function earth(){
    setHomeSelected(false);
}
</script>

 Things to note:

1. You must have public setter method defined in your constructor, like this:

 

public Boolean HomeSelected{set;}

or

Boolean HomeSelected;
public void setHomeSelected(Boolean value){
     this.HomeSelected = value;
}

 2. Call to setHomeSelected from JavaScript is asynchronous. So if you need any JavaScript operation after this call you have to use callback mechanism. Like this,

 

<apex:actionFunction name="setHomeSelected" reRender="none" oncomplete="myCallbackMethod();">
     <apex:param name="x" value="true" assignTo="{!HomeSelected}"/>
</apex:actionFunction>

Now if you have a JavaScript method defined, it will be called once the setter of HomeSelected returns.

 

<script>
function myCallbackMethod(){
alert("hi");
}
</script>

3. Rerender attribute in actionfunction declaration signifies that it is one Ajax request and the associated component will get rerendered after completion of the call. If you don't use this attribute, it will be one post request and your page will get refreshed. I have used rerender="none". This one I use when I need to do asynchronous request and I don't have anything to rerender. This will work normally provided you don't have any component with ID "none". If you have one, please use something else instead of none.

This was selected as the best answer
christwin123christwin123

Thank you so much Akram.

 

Ill try this and let you when happened soon.

 

Regards,

Christwin

christwin123christwin123

It Worked!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

 

Thanks,

Christwin