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 value from Javascript

Hi,

 

I have one inputtextbox and one outputtext. Initially both will show same value. But when i click the button my outputtext should show what i am passing in javascript which is not happening presently. Kindly help and let me know where i have mistaken.

 

My VF Code - 

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

<apex:page controller="text">
<apex:form >
<apex:actionFunction name="myJSAction" action="{!myJSAction}" rerender="mypanel">
<apex:param name="firstparam" assignTo="{!myparam}" value="" />

</apex:actionFunction>

<apex:outputpanel id="mypanel">
<apex:inputText value="{!myparam}"/>
<apex:commandButton id="button" value="click" onclick="innerJavascript()"/> <br/>
<apex:outputText value="{!myparam}"></apex:outputText> <!-- <a href="javascript&colon;innerJavascript()">Invoke action here</a> -->
</apex:outputpanel>

<script>
function innerJavascript(){
alert('Start invoking vf action!');

myJSAction('Value is changed by action called by Javascript in Visualforce.');
}
</script>
</apex:form>
</apex:page>

 

 

My Controller ------

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

public class text{

public string myparam {get; set;}
public string myparam1 {get;set;}

public text(){
myparam = 'Initial value';

}

public PageReference myJSAction(){

return null;
}
}

 

Regards.

Avidev9Avidev9
<apex:page controller="text">
   <apex:form >
      <apex:actionFunction name="myJSAction" action="{!myJSAction}" rerender="mypanel">
         <apex:param name="firstparam" assignTo="{!myparam}" value="" />
      </apex:actionFunction>
      <apex:outputpanel id="mypanel">
         <apex:inputText value="{!myparam}"/>
         <apex:commandButton id="button" value="click" onclick="innerJavascript();return false;"/>
         <br/>
         <apex:outputText value="{!myparam}"></apex:outputText>
         <!-- <a href="javascript&colon;innerJavascript()">Invoke action here</a> -->
      </apex:outputpanel>
      <script>
         function innerJavascript(){
         alert('Start invoking vf action!');
         myJSAction('Value is changed by action called by Javascript in Visualforce.');
         }
      </script>
   </apex:form>
</apex:page>

 Probably page is getting reloaded. try the above code.

 

adding return false...will prevent the default action of command button!

ethan huntethan hunt

Hi Avi,

 

Thnks for the solution. But i want to display the javascript value only in Outputtext not in inputtext . Do i have make changes in my controller.

 

Kindly help.

 

Regards

Avidev9Avidev9

So that means both the fields should refer to two different variables.

<apex:page controller="text">
   <apex:form >
      <apex:actionFunction name="myJSAction" action="{!myJSAction}" rerender="mypanel">
         <apex:param name="firstparam" assignTo="{!myparam}" value="" />
      </apex:actionFunction>
      <apex:outputpanel id="mypanel">
         <apex:inputText value="{!myparam}"/>
         <apex:commandButton id="button" value="click" onclick="innerJavascript();return false;"/>
         <br/>
         <apex:outputText value="{!myparam1}"></apex:outputText>
         <!-- <a href="javascript&colon;innerJavascript()">Invoke action here</a> -->
      </apex:outputpanel>
      <script>
         function innerJavascript(){
         alert('Start invoking vf action!');
         myJSAction('Value is changed by action called by Javascript in Visualforce.');
         }
      </script>
   </apex:form>
</apex:page>

 

 

 

 

public class text {
    public string myparam {
        get;
        set;
    }
    public string myparam1 {
        get;
        set;
    }
    public text() {
        myparam = 'Initial value';

    }
    public void myJSAction() {

       myparam1 = myparam;
       myparam ='';
    }
}

 

 

ethan huntethan hunt

Hi,

 

Now the value ' Initial value is coming in both input and output.

But output should display only 'Value is changed by action called by Javascript in Visualforce.' invoked by javascript.

 

regards

ethan huntethan hunt

Hi,

 

Thnks for your help .I got the correct output what i need.

 

 

Regards

Sidhartha

Avidev9Avidev9
Please mark it as solution if it solves your problem.