function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ethan huntethan hunt 

Passing values using Javascript and apex:actionfuntion

Hi,

 

I am doing a simple add function using Javascript and actionfunction.Could you please help me achieveing this.I need to do this using both javascript and action function

 

My VF code - 

-----------------------

<apex:page controller="add">
<apex:form >
<apex:actionFunction name="doAdd" action="{!doAdd}" reRender="out">
<apex:param AssignTo="{!res}" value=""/>
</apex:actionFunction>
<apex:pageBlock >
<apex:inputText id="test" value="{!tex1}"/>
<apex:commandButton value="Click" onclick="doAd();return false"/>
</apex:pageBlock>

<apex:outputPanel id="out">
<apex:outputText value="{!res}">

</apex:outputText>
</apex:outputPanel>

<script>
function doAd()
{
var a = document.getElementById('{!$Component.tex1}').value
}
</script>
</apex:form>
</apex:page>

 

My Controller - 

-----------------------------------

public with sharing class add
{
public integer tex1{get;set;}
public decimal res{get;set;}
public boolean show { get; set; }

public add() {

show = false;
}
public PageReference doAdd() {
show = true;

res = (tex1+ 1);
return null;
}
}

 

Regards

Best Answer chosen by Admin (Salesforce Developers) 
Puja_mfsiPuja_mfsi

Hi,

Copy the below code .

You need to call doAdd() method inside the java script method with parameter

 

 

<apex:page controller="AddController" id="thePage">
<apex:form id="theForm" >
<apex:pageBlock id="theBlock" >
<apex:inputText id="test" value="{!tex1}"/>
<input type="button" value="Click" onclick="doAd()"/>
</apex:pageBlock>

<apex:outputPanel id="out">
<apex:outputText value="{!res}"/>
</apex:outputPanel>
<apex:actionFunction name="doAdd" action="{!doAdd}" reRender="out" >
<apex:param name="firstParam" assignTo="{!res}" value="" />
</apex:actionFunction>
</apex:form>
<script type="text/javascript">
function doAd(){
var a = document.getElementById('thePage:theForm:theBlock:test').value ;
doAdd(a);
}
</script>
</apex:page>

 

If this post is helpful please give kudos(click on start at left),Please let me know if there is any issue.

All Answers

Avidev9Avidev9
Instead of calling "doAd" call "doAdd" from your command button
asish1989asish1989

HI

can you tell me why do you want to do add function by using javascript and and action function?

This is not necessary.

It can be easily done by using simply commandbutton.

for example 

<apex:commandButton value="Click"  action="{!doAdd}" rerender= "out"/>

 

If you want to invoke through javascript then , you add this line inside java script function.

<script>
function doAd()
{
var a = document.getElementById('{!$Component.tex1}').value

doAdd();
}
</script>

 

 

If this post helps you give kudos and accept it as solution

 

Thanks

Puja_mfsiPuja_mfsi

Hi,

Copy the below code .

You need to call doAdd() method inside the java script method with parameter

 

 

<apex:page controller="AddController" id="thePage">
<apex:form id="theForm" >
<apex:pageBlock id="theBlock" >
<apex:inputText id="test" value="{!tex1}"/>
<input type="button" value="Click" onclick="doAd()"/>
</apex:pageBlock>

<apex:outputPanel id="out">
<apex:outputText value="{!res}"/>
</apex:outputPanel>
<apex:actionFunction name="doAdd" action="{!doAdd}" reRender="out" >
<apex:param name="firstParam" assignTo="{!res}" value="" />
</apex:actionFunction>
</apex:form>
<script type="text/javascript">
function doAd(){
var a = document.getElementById('thePage:theForm:theBlock:test').value ;
doAdd(a);
}
</script>
</apex:page>

 

If this post is helpful please give kudos(click on start at left),Please let me know if there is any issue.

This was selected as the best answer
ethan huntethan hunt

Thank you Puja very much....