+ Start a Discussion
Div403Div403 

Passing values from Inputhidden to javascript

Hi All,

I am unable to pass the values from input hidden values to javascript function.

In javascript i am not able to get the URL from controller. When i click button, alert box is not displaying.

Please find the below code
VisualForce Page Code
<apex:page controller="ExampleController" action="{!ExampleMethod}">
<script>

function URL()
{
    var URL = document.getElementById('{!$Component.theHiddenInput}').value;

    alert('URL'+URL);
}
</script>
<apex:form >
  <apex:pageMessages />
  <apex:inputHidden value="{!values}" id="theHiddenInput"/>
<apex:commandButton onclick="URL()" value="OK" rerender="e"/>
 </apex:form>
</apex:page>

Apex Code

public with sharing class ExampleController{
Public string values{get; set;}
public ExampleController(){
values = System.currentPageReference().getParameters().get('URL');
System.debug('URL'+values);
}

public pagereference ExampleMethod(){
Public String URL='https://google.co.in';
PageReference Page=new PageReference(URL);
         Page.Redirect(true);
         return Page;
}
}
 
Swayam  AroraSwayam Arora
Hi Div043,

Below is the updated code. I have changed the function name to meke the code more readable.

<apex:page controller="ExampleController" action="{!ExampleMethod}" id="myPage">
<script>

function getURL()
{
    var URL = document.getElementById('myPage:myForm:theHiddenInput').value;
    alert('URL'+URL);
}
</script>
<apex:form id="myForm">
  <apex:pageMessages />
  <apex:inputHidden value="{!values}" id="theHiddenInput"/>
<apex:commandButton onclick="getURL()" value="OK" rerender="e"/>
 </apex:form>
</apex:page>

Please mark the answer as Best Answer if the code really helped so that it help others as well.
Div403Div403
Hi Swayam,

It is not working.
Swayam  AroraSwayam Arora
Controller:-
public with sharing class ExampleController{
Public string values{get; set;}
public ExampleController(){
    values = System.currentPageReference().getParameters().get('URL');
    values ='https://google.co.in';
    System.debug('URL'+values);
}

public pagereference ExampleMethod(){
String URL='https://google.co.in';
PageReference Page=new PageReference(URL);
         Page.setRedirect(true);
         return Page;
}
}

Page:-
<apex:page controller="ExampleController" id="myPage">
<script>

function getURL()
{
    var URL = document.getElementById('myPage:myForm:theHiddenInput').value;
    alert('URL'+URL);
}
</script>
<apex:form id="myForm">
  <apex:pageMessages />
  <apex:inputHidden value="{!values}" id="theHiddenInput"/>
<apex:commandButton onclick="getURL()" value="OK" rerender="e" action="{!ExampleMethod}"/>
 </apex:form>
</apex:page>


Please mark the answer as Best Answer if the code really helped so that it help others as well.
DixitDixit
did you try passing the <script> </script> to the end of the page?
and add ";" after getURL()...
onclick="getURL();"