I have a link commandLink with value and onclick attributes, When I click on the link it should call a function in JS to get commandLink value. but it returns undefined in alert. How can I get commandLink value ?

function callActionMethod(){
    var param1 = $j("#rowlink").val();

<apex:commandLink reRender="testlabel" 
                  value="{!accounts.id}" />
  • It may help if you explain why you're trying to do this, there may be smoother solutions. – Samuel De Rycke Jan 15 '14 at 12:40

You can pass the parameter directly to the function; there's no need to do this with jQuery (or any JavaScript), at all.

function callActionMethod(param) {
<apex:commandLink value="{!accounts.id}" onclick="callActionMethod('{!accounts.id}')" />

Fully functional example:

<apex:page controller="myControllerA">
function callActionMethod(recordId) {
    <apex:form id="form">
        <apex:actionFunction name="readCell" action="{!readCellMethod}" reRender="form">
            <apex:param value="" name="recordId" assignTo="{!selectedId}"/>
        <apex:repeat value="{!accounts}" var="account">
            <apex:commandLink value="{!account.name}" onclick="callActionMethod('{!account.id}'); return false;"/><br/>
  • Thank you very much for your help, but could you help me with this ? I put after alert, another method readCell(param) .. but the value was null. <apex:actionFunction name="readCell" action="{!readCellMethod}"> <apex:param name="parameter1" value="" assignTo="{!test}"/> </apex:actionFunction> – user3003810 Jan 15 '14 at 15:15
  • @user3003810 You have to cancel the commandLink's event or all bets are off. Here's an example: <apex:commandLink value="{!account.name}" onclick="updateRecord('{!account.id}'); return false;" />. The "return false" causes the link's action to be canceled (which is okay, because updateRecord will call the action method). – sfdcfox Jan 15 '14 at 16:17
  • @user3003810 Added a fully functional example (controller not included). – sfdcfox Jan 15 '14 at 16:19
  • The controller : public integer test {set; get;} public string val {set; get;} public PageReference readCellMethod(){ val = 'You have entered : 1 - '+test; } – user3003810 Jan 16 '14 at 8:32
  • thank you very much, I used actionSupport instead of actionFunction and everything is fine ! thank you for your time :) – user3003810 Jan 16 '14 at 8:58

Instead of this you can use:

<apex:commandLink reRender="testlabel" id="rowlink" onclick="callActionMethod()" 
                  value="{!accounts.id}" />

function callActionMethod(){
      var param1 = $j("#rowlink").attr('value');
  • still undefined .. – user3003810 Jan 15 '14 at 12:24
  1. Command Link's "value" isn't an actual "value" (i.e. val() won't work, it is not an input element). use $().html() instead.
  2. You need to address the element as a "managed ID" value (e.g. {!$Component.rowlink} instead of rowlink). For example, $("[id='{!$Component.rowlink}']").
  3. If this ID is further embedded in a repeat, you probably need to use $("[id$=rowlink]") to get all row's values.
  4. You may want to use a Visualforce HTML5 data pass through if you want to store a custom attribute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.