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
Austin Davis 8Austin Davis 8 

I need to pass a simple string to the controller, but none of the methods I've tried seem to be working.

Nothing seems to be working for me for trying to pass just a single variable between a VisualForce Page and its Controlller.

Attempt 1:
public String selectedItem {get; set;}

<apex:form>
     <apex:inputhidden value="{!selectedItem}" value="" id="itemID"/>
</form>

<script type="text/javascript">
function clickedItem(s)
{
jQuery("[id*='itemID']").val(s);
alert('{!selectedItem}'};
}
</script>

<tr onclick="clickedItem">
//................................
</tr>

Attempt 2:
public String selectedItem {get; set;}

public PageReference clickedItem(){
        selectedItem = ApexPages.currentPage().getParameters().get('itemID');
        System.Debug('Selected Item = ' + selecteditem);
        return null;
    }

<apex:form>
<apex:actionFunction action="{!clickedItem}" name="setSelectedItem">
<apex:param name="itemID" value="" assignto="{!selectedItem}"/>
</apex:actionFunction>
<apex:form>

If I try a normal actionfunction with set off by an onclick event then the parameter does not pass. I have been informed that I need to have the rerender attribute assigned. However, when I put in the rerender attribute I get the following error:
Uncaught SyntaxError: Unexpected token :

Any help is very much appreciated. Thanks.
Alain CabonAlain Cabon
Hello,

This code works. Look at the positions of the "apex:form" tags.
 
public class TestParam1 {
    public String selectedItem {get; set;}
    public  TestParam1() {
        selectedItem = 'First';
    }
    public PageReference clickedItem(){
        //  selectedItem = ApexPages.currentPage().getParameters().get('itemID');
        System.Debug('Selected Item = ' + selecteditem);
        return null;
    }   
}
 
<apex:page controller="TestParam1" >
    <apex:outputPanel id="showstate">{!selectedItem}</apex:outputPanel>
    <apex:outputPanel onclick="setSelectedItem('Second')" styleClass="btn">Click Me</apex:outputPanel>  
    <apex:form >
        <apex:actionFunction action="{!clickedItem}" name="setSelectedItem"  rerender="showstate">
            <apex:param name="itemID" value="" assignto="{!selectedItem}"/>
        </apex:actionFunction>        
    </apex:form>
</apex:page>

Alain